Box2d/LibGDX: 沿精灵的原点旋转夹具。见插图

Box2d/LibGDX: Rotate fixture along sprite's origin. See illustration

如果我 applyAngularImpulse 这个圆精灵的 body,它会沿着它的中心旋转,这就是我想要发生的事情。但是它的碰撞遮罩夹具(绿色)沿着它自己的中心而不是精灵的中心旋转。我怎样才能让它也沿着 sprite 的中心旋转,以便碰撞遮罩和 sprite 保持同步?

例如,在下图中,当圆圈旋转并且红色部分到达左上角时,我希望绿色碰撞遮罩位于位置 #3。但是#2发生了。我知道为什么会这样,但我怎样才能实现我在这里想要完成的目标?

您需要创建两个 fixture - 一个大小为 sprite(并且 sprite 应该以这个 fixture 为中心),第二个移动到圆的边缘(您当前的碰撞 fixture)

BodyDef bodyDef;

...

Body body = world.createBody(bodyDef);

FixtureDef circleFixture = new FixtureDef();
FixtureDef collisionFixture = new FixtureDef();

CircleShape circle = new CircleShape();
circle.setRadius(spriteRadius);
circleFixture.shape = circle;

//same for collision fixture with proper shape - (0,0) i a center of circle
float[] vertices = new float[]{x1, y1, x2, y2...};

ChainShape chain = new ChainShape();
chain.createLoop(vertices);
fixtureDefs.peek().shape = chain;

现在,如果您需要将碰撞装置保持在圆的边缘,只需定义将其放置在那里的点 - 例如,对于半径为 2 个顶点

的圆
{5, -1, 6, 0, 5, 1, 4, 0}

会给出类似

的东西

旋转后它应该表现得像

当然你需要在圆夹具的中心渲染你的精灵