Box2D 远离行星旋转

Box2D rotating away from planet

所以我试图让我的玩家 body 旋转离开,但是当在行星的下半部分旋转反转时,我使用 .setTransform() 进行旋转。

if(insideRotation==Math.abs(playerRotation)){

        } else if(insideRotation<Math.abs(playerRotation)){
            insideRotation+=Math.PI/45;
        } else if (insideRotation>Math.abs(playerRotation)){
            insideRotation-=Math.PI/45;
}

方块上的线显示了它面向的方向,我希望底部的图片背对着绿色圆圈。

如果你想让一个 body 总是远离行星中心,你可以将它的矢量旋转设置为 body 中心和行星中心之间的减法结果。

会是这样的

    Body body, planet; //your 'character' and planet

    ...

    Vector2 bodyCenter = body.getWorldCenter();
    Vector2 planetCenter = planet.getWorldCenter(); //if you would use getPosition it would be related to the body's origin!

    Vector2 subVector = bodyCenter.sub( planetCenter );

    body.setTransform(x, y, subVector.angle() ); //it is possible that you will need to make '-angle()' here or something!

这样,无论 body 在行星上的哪个位置,您都将始终 "fixed to planet" 自转。