java - Box2D 远离行星旋转

标签 java rotation libgdx box2d

所以我试图让我的玩家 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;
}

Facing the wrong way

Facing the righ way

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

最佳答案

如果你想让物体始终远离行星中心,你可以将其旋转 vector 设置为物体中心和行星中心之间的减法结果。

大概是这样的

    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 在行星上处于什么位置,您都将始终“固定到行星”旋转。

关于java - Box2D 远离行星旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34293162/

相关文章:

java - Spring Security ACL继承: How does it really works?

UIImageView 上的 iOS CAKeyframeAnimation rotationMode

java - LibGDX标度法的使用

java - LibGdx - 缩放 Sprite 、相机和视口(viewport)

java围绕中心旋转矩形

LibGDX - 使用着色器将纹理覆盖在另一个纹理之上

java - 为什么写 "t"语法名时 'Hashtable'字母写小了?

java - 在方法中使用通用谓词和函数

java - 是否有 OSGi 的非 Java 替代品?

three.js - 角色定位 ThreeJS