Mustafa
Mustafa

Reputation: 177

Box2d fixture position

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.

Upvotes: 3

Views: 7859

Answers (2)

Abhineet Prasad
Abhineet Prasad

Reputation: 1271

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.

Upvotes: 2

NotNowLewis
NotNowLewis

Reputation: 91

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.

Upvotes: 9

Related Questions