Libgdx Sprite围绕特定点旋转

我做了一个复杂的dynamic的身体,围绕着静止的其他身体旋转。 它完美的工作,但是我弄乱了身体上的精灵位置。 这是我现在拥有的 在这里输入图像说明

这是我需要的

在这里输入图像说明

我将添加我的代码

private Body createSide(float x, float y, Sprite tmpSprite, boolean sharp){ BodyDef bDef = new BodyDef(); bDef.type = BodyDef.BodyType.DynamicBody; PolygonShape shape = new PolygonShape(); if(!sharp){ shape.setAsBox(1.8f / PPM, 0.2f / PPM); } else { shape.setAsBox(0.2f / PPM, 1.8f / PPM); } FixtureDef fdef = new FixtureDef(); fdef.shape = shape; bDef.position.set(x, y); Body body = world.createBody(bDef).createFixture(fdef).getBody(); body.setAngularVelocity(-1f); tmpSprite.setPosition(bDef.position.x * 32, bDef.position.y * 32); return body; } private void createBody(int y, final int V_WIDTH){ staticBody = createStaticBody(y, V_WIDTH); Body rightSide = createSide(5.5f, 15, sprites[0], true); Body leftSide = createSide(9.5f, 15, sprites[1], true); Body top = createSide(7.5f, 17, sprites[2], false); Body bottom = createSide(7.5f, 13, sprites[3], false); bodies[0] = rightSide; bodies[1] = leftSide; bodies[2] = top; bodies[3] = bottom; ... ... ... } 

这是我的绘制方法

 @Override public void draw(SpriteBatch sb) { for (int i=0; i<bodies.length; i++){ sprites[i].setRotation(bodies[i].getAngle() * MathUtils.radiansToDegrees); sprites[i].draw(sb); } } 

好吧,经过4个小时的search,终于find了错误。 问题在于精灵的起源。 这是必要的行(staticBody是身体周围的其他身体将旋转)

 tmpSprite.setOrigin(staticBody.getPosition().x / 32 + tmpSprite.getWidth() / 2, staticBody.getPosition().y / 32 + tmpSprite.getHeight() / 2); 

这里是完整的代码

 private Body createSide(float x, float y, Sprite tmpSprite, boolean sharp){ BodyDef bDef = new BodyDef(); bDef.type = BodyDef.BodyType.DynamicBody; PolygonShape shape = new PolygonShape(); if(!sharp){ shape.setAsBox(1.8f / PPM, 0.2f / PPM); } else { shape.setAsBox(0.2f / PPM, 1.8f / PPM); } FixtureDef fdef = new FixtureDef(); fdef.shape = shape; bDef.position.set(x, y); Body body = world.createBody(bDef).createFixture(fdef).getBody(); body.setAngularVelocity(1f); //body.setFixedRotation(true); tmpSprite.setX(body.getPosition().x * 32 - tmpSprite.getWidth()); tmpSprite.setY(body.getPosition().y * 32 - tmpSprite.getHeight()); tmpSprite.setOrigin(staticBody.getPosition().x / 32 + tmpSprite.getWidth() / 2, staticBody.getPosition().y / 32 + tmpSprite.getHeight() / 2); return body; } @Override public void draw(SpriteBatch sb) { for (int i=0; i<sprites.length; i++){ float xball = 32 * (bodies[i].getPosition().x) - sprites[i].getWidth()/2; float yball = 32 * (bodies[i].getPosition().y) - sprites[i].getHeight()/2; sprites[i].setRotation(bodies[i].getAngle() * MathUtils.radiansToDegrees); sprites[i].setPosition(xball, yball); sprites[i].draw(sb); } }