How to get a fixture's Shape's points from a Body* in box2D?
Hey i'm trying to integrate Box2D and SFML, and my class takes in a Body pointer, which i need to use to get all the points of a fixture so i can form a gr开发者_开发问答aphical representation of the body out of them.
How do i get these points?
You can iterate over the fixtures in a body like this:
for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext())
{
....
}
Once you have a fixture you will need to check what kind of shape it has, then cast to that shape type to access the shape data:
b2Shape::Type shapeType = fixture->GetType();
if ( shapeType == b2Shape::e_circle )
{
b2CircleShape* circleShape = (b2CircleShape*)fixture->GetShape();
...
}
else if ( shapeType == b2Shape::e_polygon )
{
b2PolygonShape* polygonShape = (b2PolygonShape*)fixture->GetShape();
....
}
Use GetVertexCount() and GetVertex() to get the vertices from a polygon shape.
Note that vertex positions stored in the fixture are in body coordinates (relative to the body that the fixture is attached to). To get locations in world coordinates, you would have to multiply by the body transform:
b2Vec2 worldPos = body->GetWorldPoint( localPos );
精彩评论