如何在Libgdx Box2d中获取一个物体的每个fixture的位置?
看起来fixture没有位置getter。如果这个问题很菜,请原谅,因为我刚开始学习Box2d。
一旦你了解了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!
很简单!
但是如果你有一个多边形而不是圆形呢?同样很简单!只需要将变换应用到图形中的每个顶点即可。
从box2d物体中获取所有fixture的列表。 对于每个fixture,获取其形状。 如果该形状是CircleShape类型,则可以使用getPosition()方法。然而,检索到的位置是相对于b2World中box2d物体的位置。
body.setPosition()
方法会将物体在 b2World 中的位置设置为指定值吗?另外,getPosition()
方法返回的是形状中心点的相对位置还是左上角位置的相对位置? - newguy