(Java)将四元数转换为弧度(或度)?

时间:2012-10-15 08:30:26

标签: java opengl quaternions bulletphysics radians

过去几天我一直在乱用OpenGL和JBullet。经过大量的修补,我终于设法修复了JBullet中的一些错误,并使其可用于一些演示。我目前遇到的问题是我无法将翻译转换为Degrees,到目前为止,我已经使用以下代码对Quaternions,Radians进行了最佳结果:

public Vector3f getRadians(Quat4f quat) {
    Vector3f v = new Vector3f();
    //float scale = (float) Math.abs(quat.x * quat.y * quat.z); //Apparently the scale of the angle, thought without I get a almost perfect result on the X-Axis
    float angle = (float) Math.abs(Math.acos(quat.w)) * 2.0f;

    v.x = angle * Math.round(quat.x);
    v.y = angle * Math.round(quat.y);
    v.z = angle * Math.round(quat.z);               

    return v;
}

值得注意的是,我唯一的成功将是四元数的X轴,并且在任何其他轴上都没有翻译。更不用说它在0-6度之间。

1 个答案:

答案 0 :(得分:1)

我真的不确定这是你想要的,但是来自glm :: gtx :: quaternion:

inline valType roll
(
    detail::tquat<valType> const & q
)
{
    return atan2(valType(2) * (q.x * q.y + q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z);
}

template <typename valType> 
inline valType pitch
(
    detail::tquat<valType> const & q
)
{
    return atan2(valType(2) * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z);
}

template <typename valType> 
inline valType yaw
(
    detail::tquat<valType> const & q
)
{
    return asin(valType(-2) * (q.x * q.z - q.w * q.y));
}

所以,以更易读的形式:

float roll = atan2(valType(2) * (q.x * q.y + q.w * q.z), q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z);
float pitch = atan2(valType(2) * (q.y * q.z + q.w * q.x), q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z);
float yaw = asin(valType(-2) * (q.x * q.z - q.w * q.y));
vec3 EulerAngles = vec3(pitch, yaw, roll);