Java 从两个方向向量获取euler旋转

Java 从两个方向向量获取euler旋转,java,opengl,rotation,linear-algebra,Java,Opengl,Rotation,Linear Algebra,我目前正在进行三维重力模拟。我有两个向量来表示立方体正面和正面的法向量 向上-u 前-v 如何获得该立方体的欧拉旋转? 我尝试将u和v投影到使用XYZ轴作为法向量的平面上,并获取XYZ轴和投影向量之间的角度,以获得沿XYZ轴的旋转 rotation.x = Vector.projectOnPlane( new Vector3f(gravityVec).negate(), //pointing upward new Vector3f(1, 0, 0)).norm

我目前正在进行三维重力模拟。我有两个向量来表示立方体正面和正面的法向量

  • 向上-
    u

  • 前-
    v

如何获得该立方体的欧拉旋转?

我尝试将
u
v
投影到使用XYZ轴作为法向量的平面上,并获取XYZ轴和投影向量之间的角度,以获得沿XYZ轴的旋转

rotation.x = Vector.projectOnPlane(
        new Vector3f(gravityVec).negate(), //pointing upward
        new Vector3f(1, 0, 0)).normalize() //project onto yz-plane
        .angle(new Vector3f(0, 1, 0)) //angle to y-axis

rotation.y = Vector.projectOnPlane(
        new Vector3f(zDir).negate(), //pointing forward
        new Vector3f(0, 1, 0)).normalize() //project onto xz-plane
        .angle(new Vector3f(1, 0, 0)) //angle to x-axis

rotation.z = Vector.projectOnPlane(
        new Vector3f(gravityVec).negate(), //pointing upward
        new Vector3f(0, 0, 1)).normalize() //project onto xy-plane
        .angle(new Vector3f(0, 1, 0)) //angle to y-axis
其中函数
projectOnPlane

public static Vector3f projectOnPlane(Vector3f vec, Vector3f planeNormal)
{//project vec on a plane that use planeNormal as normal vector
    return new Vector3f(vec).sub(new Vector3f(vec).mul(new Vector3f(planeNormal))
            .div(planeNormal.length() * planeNormal.length()).mul(planeNormal)).normalize();
}
但这并没有达到我的期望

当x和z都接近于0时,它工作。但当我搬到另一个地方时,它出了问题


什么是“它工作不正常”的意思?你有什么错误吗?结果和期望值是什么?@Amongalen结果不是我期望的结果。然后编辑问题并添加结果和你期望的结果。