簡體   English   中英

(Java)將四元數轉換為弧度(或度)?

[英](Java) Converting a Quaternion into Radians (Or Degrees)?

最近幾天,我一直在OpenGL和JBullet中搞亂。 經過大量的修改,我終於設法修復了JBullet中的許多錯誤,並使其可用於某些演示。 我目前遇到的問題是我無法設法將翻譯轉換為度數,到目前為止,使用四元數轉換為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度之間的任何位置都有偏移。

我真的不確定這是您想要的,但是從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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM