你可以像这样迭代一个体内的灯具:
for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext())
{
....
}
Run Code Online (Sandbox Code Playgroud)
一旦你有一个夹具你需要检查它有什么样的形状,然后转换为该形状类型来访问形状数据:
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();
....
}
Run Code Online (Sandbox Code Playgroud)
使用GetVertexCount()和GetVertex()从多边形形状中获取顶点.
请注意,存储在夹具中的顶点位置在主体坐标中(相对于夹具所连接的主体).要获取世界坐标中的位置,您必须乘以体变换:
b2Vec2 worldPos = body->GetWorldPoint( localPos );
Run Code Online (Sandbox Code Playgroud)