0
\$\begingroup\$

I have two objects. A dynamic player and a static ground object.

It was working for a while until I did some refactoring and suddenly my play falls right through my ground object.

Is there anything obviously wrong with the following? I have no idea what might be wrong so if you have experienced this before, enlighten me!

Here's the initialization code of the objects:

(pixelsPerMeter is 10)

    int pixelsPerMeter = Game.instance.pixelsPerMeter;

    //create body def
    BodyDef boxDef = new BodyDef();
    boxDef.position.set(0, 0);
    boxDef.type = BodyType.DYNAMIC;

    //create shape
    PolygonShape boxShape = new PolygonShape();
    boxShape.setAsBox(this.size.width / pixelsPerMeter, this.size.height / pixelsPerMeter);

    //create body
    body = Game.instance.world.createBody(boxDef);

    //finally create the fixture
    FixtureDef boxFixture = new FixtureDef();
    boxFixture.density = 1;
    boxFixture.shape = boxShape;

    //add the fixture to the box? :\
    body.createFixture(boxFixture);

This is done twice, one for the player and once for the ground object.

The player's body type is set to BodyType.DYNAMIC while the ground is STATIC.

And of course the ground is below the body, and gravity is set to 9.8f.

\$\endgroup\$
  • \$\begingroup\$ You might have a problem with this.size.width / pixelsPerMeter if this.size.width is an integer then the division will be done using integer arithmetic and round towards zero. If it is less than 10 then it will be zero (the same applies to height). \$\endgroup\$ – GuyRT Apr 22 '14 at 14:25
1
\$\begingroup\$

Turns out, there was two problems. One was I set the size property of my entity after instantiating but did not update the Body size.

The other issue is I didn't realize box2d positions was centered for each body.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.