I develop jbox2d (java) and I have a problem when I create a polygon shape.
I can not choose the position of my polygon, there is a field "m_centroid" I defined as the center of my window. The polygon remains in the left corner.
public class Player {
private Body body;
public Player(World world) {
BodyDef bodyDef = new BodyDef();
bodyDef.angle = (float) Math.toRadians(0.0f);
bodyDef.position.set(800.0f / 2, 600.0f / 2);
bodyDef.type = BodyType.DYNAMIC;
/*Vec2[] vertices = {
new Vec2(bodyDef.position.x, bodyDef.position.y - 10.0f),
new Vec2(bodyDef.position.x + 10.0f, bodyDef.position.y + 10.0f),
new Vec2(bodyDef.position.x - 10.0f, bodyDef.position.y + 10.0f)
};*/
Vec2[] vertices = {
new Vec2(0.0f, - 10.0f),
new Vec2(+ 10.0f, + 10.0f),
new Vec2(- 10.0f, + 10.0f)
};
PolygonShape shape = new PolygonShape();
shape.set(vertices, vertices.length);
shape.m_centroid.set(bodyDef.position);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.3f;
fixtureDef.restitution = 0.5f;
body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
}
}
The centroid is supposed to be the center of the polygon in local coordinates, and is used for torque/mass calculations. You shouldn't be modifying that. Instead, you need to use the transform on the Body. Try something like
body.getTransform().setPos(___)
.