1
votes

I have a body with just one fixture attached to it. The shape attached to the fixture is a PolygonShape. For rendering the body, I need access to its vertices' coordinates.

This is what I tried:

Vector2 tmpVector = new Vector2();
Fixture f = body.getFixtureList().get(0);
PolygonShape shape = (PolygonShape)f.getShape();
shape.getVertex(3, tmpVector);
shape.getVertex(2, tmpVector);
shape.getVertex(1, tmpVector);
shape.getVertex(0, tmpVector);

It works when the body is not in contact with other bodies.

The problem is that when the body collides with another body, getFixtureList returns more than one fixture and this includes those from other bodies.

How do I solve this problem?

All I need to be able to do is access the vertices' positions of the polygon body on the fly.

1

1 Answers

1
votes

You can check if the fixture's body is equal to body with the getBody() method:

Vector2 tmpVector = new Vector2();
Fixture f = body.getFixtureList().get(0);
while (f.getBody() != body)
{
    f = f.getNext();
}
if (f != null)
{
    PolygonShape shape = (PolygonShape)f.getShape();
    // get vertices
}

Of course this work only if you know that body has exactly one fixture with a polygon shape, as it is said in the question. Otherwise you might consider using the userData attribute to store information about the different fixtures.