Box2d夹具位置

时间:2013-10-06 17:45:37

标签: java box2d libgdx

如何在Libgdx Box2d中获取一个身体的每个夹具的位置? 看起来固定装置没有位置吸气剂。 Sory如果这个问题是noobish但我刚开始学习Box2d。

2 个答案:

答案 0 :(得分:9)

一旦您了解Transform

,这很容易

让我们以圆形夹具为例,因为它们最容易展示。

// 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!

容易!

但是如果你有一个多边形而不是一个圆呢?再简单!只需将变换应用于形状中的每个顶点。

答案 1 :(得分:1)

从你的box2d身体获取所有灯具的列表。 对于每个夹具,获取其形状。 如果形状是CircleShape类型,那么您可以使用getPosition()方法。但是,检索到的位置是相对于b2World中box2d主体的位置。