1
\$\begingroup\$

I' having a problem rotating a body with setTransform(), The body has multiple sensors that should rotate with the player. The rotation works but it rotates around the bodys local 0,0 position instead of the center. Note that the game is in a top-down perspective and the player can go in four different directions, thus I need to rotate him immediately (in one tick) in 90 degrees steps.

Up:

enter image description here

Down:

enter image description here

I can't find a way to set the rotation center.

Here's the code I use to rotate it:

float angle = direction * 90 * MathUtils.degRad; // direction is an int value from 0 to 3
body.setTransform(body.getPosition(), angle);

I also tried body.getLocalCenter().set() and body.getMassData().center.set() but it didn't seem to have any effect. How can I rotate the body around its center?

EDIT (added code to create fixtures):

This is the method to create a fixture:

private Fixture createFixture(FixtureDef fixtureDef, Body body) {
    PolygonShape ps = createPolygon(); // creates a polygon shape from XML config
    fixtureDef.shape = ps;
    Fixture fixture = body.createFixture(fixtureDef);

    if (fc.filter != null) {
        fixture.setFilterData(fc.filter);
    }
}

This is the simplified code to create the fixtures (they are really loaded with xml config but these are the values I set):

FixtureDef sensorDef1 = new FixtureDef();
sensorDef1.friction = 0;
sensorDef1.restitution = 0;
sensorDef1.isSensor = true;
sensorDef1.center = new Vector2(0.8f, 1.6f);
// and some filters

FixtureDef sensorDef2 = new FixtureDef();
sensorDef2.isSensor = true;
sensorDef2.center = new Vector2(0.8f, 1.6f);
// and some filters

FixtureDef sensorDef3 = new FixtureDef();
sensorDef3.isSensor = true;
// and some filters

createFixture(sensorDef1, body);
createFixture(sensorDef2, body);
createFixture(sensorDef3, body);
\$\endgroup\$
  • \$\begingroup\$ Please share the code you use to create the sensors. \$\endgroup\$ – ClassicThunder Aug 21 '14 at 20:33
1
\$\begingroup\$

I fixed the issue by changing the global coordinate system to the box2d one. Now the local 0,0 coordinates of my entities correspond to their center coordinates and they represent meters instead of pixels.

EDIT (some clarifications):

I now translate them in the render system (because this is really the only place where I need pixel coordinates):

// get the position from the entity, scale it by PIXELS_PER_METER
// and subtract half the dimension (which is also scaled by PIXELS_PER_METER)
Vector2 scaledPos = pos.cpy().scl(Config.PIXELS_PER_METER)
            .sub(dimension.cpy().scl(0.5f * Config.PIXELS_PER_METER));

Not that in my case Config.PIXELS_PER_METER is equal to 10.0f.

In every other place where I use coordinates, they are already configured as meter and the coordinate represents the center of a body, just like in Box2D. This way the body rotates around the center of the body instead of 0,0.

\$\endgroup\$
  • 1
    \$\begingroup\$ Please expand on how you did the changing the global coordinate system to the box2d one bit preferably with a bit of code and then mark your answer as accepted if it resolves your issue, \$\endgroup\$ – ClassicThunder Aug 23 '14 at 0:15
  • \$\begingroup\$ I cannot accept my own answer within 20 hours, otherwise I would've already done that... \$\endgroup\$ – thobens Aug 23 '14 at 14:01
  • \$\begingroup\$ Opps I always forget about that. Thanks for expanding upon your answer. \$\endgroup\$ – ClassicThunder Aug 23 '14 at 19:58

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.