3
votes

How to get position of every fixture of one body in Libgdx Box2d? It seems like fixtures do not have position getter. Sory if this question is noobish but i just started learning Box2d.

2

2 Answers

1
votes

From your box2d body get the list of all fixtures. For each fixture get its shape. If the shape is of type CircleShape then you have a getPosition() method that you can use. However, the position retrieved is relative to the position of the box2d body in the b2World.

9
votes

This is easy once you know about Transform.

Let's take a circular fixture as an example, as they're the easiest to demonstrate with.

// we need to get the body's position, let's use a Vector2 to store it.
Vector2 vec = new Vector2();
Body body = fixture.getBody();

// what is this magic? Why, it's a wonderful object that transforms fixture
// position information based on the body's position!
Transform transform = body.getTransform();

CircleShape shape = (CircleShape) fixture.getShape();
vec.set(shape.getPosition());
// apply the transformation
transform.mul(vec);
// now vec.x and vec.y will be what you want!

Easy!

But what if you have a polygon instead of a circle? Easy again! Simply apply the transform to each vertex in the shape.