Erroneous challenge of the method createFixture in Box2D



  • Decided to study the physical movement Box2D in LibGDX. Most of the methods in it are naive, and it's very difficult to trace the mistake. I create a physical body, and I attach Fixture to it in QuantumCollider designer without parameters:

    World world;
    BodyDef commonBodyDef;
    FixtureDef commonBodyFix;
    WorldShell shell;
    Box2DDebugRenderer r;
    Array<Body> bodies;
    

    public QuantumCollider(String map) throws FileNotFoundException {
    bodies = new Array<Body>();
    r = new Box2DDebugRenderer();
    world = new World(new Vector2(0.0f, 0.0f), true);
    FileHandle mapFile = Gdx.files.internal("maps/" + map + ".qmap");
    if(!mapFile.exists())
    throw new FileNotFoundException("Can't find a map: " + map);
    Scanner scanner = new Scanner(mapFile.read());
    Body newWall;
    BodyDef newWallDef = new BodyDef();
    newWallDef.type = BodyType.StaticBody;
    FixtureDef newWallFix = new FixtureDef();
    PolygonShape newWallShape = new PolygonShape();
    newWallFix.shape = newWallShape;
    String newWallInfo;
    while(scanner.hasNextLine()) {
    newWallInfo = scanner.nextLine();
    newWallDef.position.set(Float.parseFloat(newWallInfo.split(" ")[0]), Float.parseFloat(newWallInfo.split(" ")[1]));
    newWallShape.setAsBox(Float.parseFloat(newWallInfo.split(" ")[2]) - newWallDef.position.x + 1, Float.parseFloat(newWallInfo.split(" ")[3]) - newWallDef.position.y + 1);
    newWall = world.createBody(newWallDef);
    newWall.createFixture(newWallFix);
    }
    scanner.close();
    newWallShape.dispose();
    commonBodyDef = new BodyDef();
    commonBodyFix = new FixtureDef();
    commonBodyDef.type = BodyType.DynamicBody;
    commonBodyDef.position.set(random(1000), random(1000));
    commonBodyFix.restitution = 1.2f;
    commonBodyFix.density = 0.01f;
    commonBodyFix.friction = 0.5f;
    CircleShape commonBodyShape = new CircleShape();
    commonBodyShape.setRadius(10.0f);
    commonBodyFix.shape = commonBodyShape;
    commonBodyShape.dispose();
    }

    public QuantumCollider() throws FileNotFoundException {
    this("ps_explosion");
    Body nobody;
    nobody = world.createBody(commonBodyDef);
    nobody.createFixture(commonBodyFix);
    nobody.applyForceToCenter(71, 71, true);
    }

    And if the body itself is well established, then when summoning the method nobody.createFixture(s), this error will erupt: введите сюда описание изображения

    I have already read about the fact that the world/thela cannot be changed at the time of the challenge of the World.step(s), but that body is created before any functions like render(s) or step(s). What else is the problem?



  • The question was asked a long time ago, but I' answer it anyway. Challenge

    nobody.createFixture(commonBodyFix);
    

    Must be before commonBodyShape.dispose(); And you have it after.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2