简体   繁体   English

Box2d夹具位置

[英]Box2d fixture position

How to get position of every fixture of one body in Libgdx Box2d? 如何在Libgdx Box2d中获得一体的每个夹具的位置? It seems like fixtures do not have position getter. 看起来固定装置没有位置吸气剂。 Sory if this question is noobish but i just started learning Box2d. 索里,如果这个问题是noobish但我刚开始学习Box2d。

This is easy once you know about Transform . 一旦你了解了Transform这很容易。

Let's take a circular fixture as an example, as they're the easiest to demonstrate with. 我们以圆形夹具为例,因为它们最容易展示。

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

Easy! 简单!

But what if you have a polygon instead of a circle? 但是如果你有一个多边形而不是圆形怎么办? Easy again! 再简单! Simply apply the transform to each vertex in the shape. 只需将变换应用于形状中的每个顶点即可。

From your box2d body get the list of all fixtures. 从你的box2d机构获取所有灯具的列表。 For each fixture get its shape . 为每个夹具得到它的形状 If the shape is of type CircleShape then you have a getPosition() method that you can use. 如果形状是CircleShape类型,那么您可以使用getPosition()方法。 However, the position retrieved is relative to the position of the box2d body in the b2World. 但是,检索到的位置是相对于b2World中box2d主体的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM