如何从 box2D 中的 Body* 获取夹具的形状点?

发布于 2024-11-18 18:54:42 字数 116 浏览 2 评论 0原文

嘿,我正在尝试集成 Box2D 和 SFML,我的类接受一个 Body 指针,我需要使用它来获取夹具的所有点,以便我可以从中形成主体的图形表示。

我如何获得这些积分?

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 graphical representation of the body out of them.

How do i get these points?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡间太子 2024-11-25 18:54:42

您可以像这样迭代主体中的固定装置:

for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext()) 
{
    ....
}

一旦有了固定装置,您将需要检查它具有哪种形状,然后转换为该形状类型以访问形状数据:

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();
    ....
}

使用 GetVertexCount() 和 GetVertex() 来获取多边形的顶点。

请注意,存储在夹具中的顶点位置采用主体坐标(相对于夹具所连接的主体)。要获取世界坐标中的位置,您必须乘以身体变换:

b2Vec2 worldPos = body->GetWorldPoint( localPos );

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 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文