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:
Down:
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);