欧拉角转换成四元数的

 

数据结构:

struct vec3 {
    vec3(){}
    vec3(float x, float y, float z) {
        mx = x; my = y; mz = z;
    }
    float mx;
    float my;
    float mz;
};

struct quat {
    quat() {}

    float mx;
    float my;
    float mz;

    float mw;
};

 

 

欧拉角有多种形式.

形式1.  绕世界系的固定xyz轴转动,顺序是 x y z

//eulerAngle is in rad;
quat EulerToQuat(const vec3& eulerAngle)
{
    
    vec3 c = vec3( cos(0.5f * eulerAngle.mx), cos(0.5f * eulerAngle.my), cos(0.5f * eulerAngle.mz) );
    vec3 s = vec3( sin(0.5f * eulerAngle.mx), sin(0.5f * eulerAngle.my), sin(0.5f * eulerAngle.mz) );

    float w = c.mx * c.my * c.mz + s.mx * s.my * s.mz;
    float x = s.mx * c.my * c.mz - c.mx * s.my * s.mz;
    float y = c.mx * s.my * c.mz + s.mx * c.my * s.mz;
    float z = c.mx * c.my * s.mz - s.mx * s.my * c.mz;

    quat res;
    res.mw = w;
    res.mx = x;
    res.my = y;
    res.mz = z;
    return res;
}

int main()
{
    std::cout << "Hello World!\n"; 

    /*
    exp:绕x轴转90度,对应四元数的向量(1,0,0),旋转角度是90度,
    quat(x,y ,z w)= 1*sin(90/2), 0, 0, cos(90/2).
    */
    quat res1 = EulerToQuat(vec3(PI/2.0f,0,0));//pass

    /*
    exp:绕x轴转0度,绕y轴转90度,对应四元数的向量(0,1,0),旋转角度是90度,
    quat(x,y ,z w)= 0,1*sin(90/2), 0, cos(90/2).
    */
    quat res2 = EulerToQuat(vec3(0, PI / 2.0f, 0));//pass

    /*
    exp:绕x轴转0度,绕y轴转0度,绕z轴转90度, 对应四元数的向量(0,0,1),旋转角度是90度,
    quat(x,y ,z w)= 0,0,1*sin(90/2), cos(90/2).
    */
    quat res3 = EulerToQuat(vec3(0, 0, PI / 2.0f));//pass

}

 

 

形式2.  .。。

posted @ 2020-08-16 13:04  JadeCicada  阅读(968)  评论(0编辑  收藏  举报