zoukankan      html  css  js  c++  java
  • 欧拉角转成四元数的公式

    这两天看MDL模型的显示代码,参考《3D数学基础:图形与游戏开发》中的公式,发现欧拉角转成四元数的公式和书中提到的公式不一致,狂晕!~~~
    void AngleQuaternion(const vec3_t Angles, vec4_t Quaternion)
        
    {
            
    float    Angle    = Angles[0* 0.5f;
            
    float    Sin0    = (float)sin(Angle);
            
    float    Cos0    = (float)cos(Angle);

            Angle            
    = Angles[1* 0.5f;
            
    float    Sin1    = (float)sin(Angle);
            
    float    Cos1    = (float)cos(Angle);

            Angle            
    = Angles[2* 0.5f;
            
    float    Sin2    = (float)sin(Angle);
            
    float    Cos2    = (float)cos(Angle);

            Quaternion[
    0]    = Sin0 * Cos1 * Cos2 - Cos0 * Sin1 * Sin2;
            Quaternion[
    1]    = Cos0 * Sin1 * Cos2 + Sin0 * Cos1 * Sin2;
            Quaternion[
    2]    = Cos0 * Cos1 * Sin2 - Sin0 * Sin1 * Cos2;
            Quaternion[
    3]    = Cos0 * Cos1 * Cos2 + Sin0 * Sin1 * Sin2;
        }


    书上的公式是这样的:

        |cos(h/2)  |
        |    0         |
    h=|sin(-h/2)  |
        |    0         |

        |cos(p/2)  |
        |-sin(p/2)  |
    p=|    0         |
        |    0         |

        |cos(b/2)  |
        |    0         |
    b=|    0         |
        |-sin(b/2)  |
                                           | cos(h/2)cos(p/2)cos(b/2)+sin(h/2)sin(p/2)sin(b/2)|        (w)
                                           |cos(h/2)sin(p/2)cos(b/2)+sin(h/2)cos(p/2)sin(b/2) |         (x)
    q惯性-物体(h,p,b) =  |sin(h/2)cos(p/2)cos(b/2)-cos(h/2)sin(p/2)sin(b/2)  |         (y)
                                           |cos(h/2)cos(p/2)sin(b/2)-sin(h/2)sin(p/2)cos(b/2)  |         (z)


    上面的代码与公式比较以后,发现有点像这样:
    Quaternion[0] = y;
    Quaternion[1] = x;
    Quaternion[2] = z;
    Quaternion[3] = w;

    这样明显是不对的,而四元数转矩阵的时候,用的四元数应该是
    Quaternion[0] = x;
    Quaternion[1] = y;
    Quaternion[2] = z;
    Quaternion[3] = w;

    颠倒顺序乘也都不对

    今天想起了前些天摘录的文章《Quaternion Powers》
    里面提到的欧拉角转换成四元数的公式:

    Euler to Quaternion

    Converting from Euler angles to a quaternion is slightly more tricky, as the order of operations must be correct. Since you can convert the Euler angles to three independent quaternions by setting the arbitrary axis to the coordinate axes, you can then multiply the three quaternions together to obtain the final quaternion.

    So if you have three Euler angles (a, b, c), then you can form three independent quaternions

    Qx = [ cos(a/2), (sin(a/2), 0, 0)]
    Qy = [ cos(b/2), (0, sin(b/2), 0)]
    Qz = [ cos(c/2), (0, 0, sin(c/2))]

    And the final quaternion is obtained by Qx * Qy * Qz.

    算出来的结果跟代码中的是一样的。竟然是两种截然不同的转换公式,狂晕一个先。具体的原因还不知道    :(,搞清楚了再贴上来。

    公式如下:
         |   cos(a/2)*cos(b/2)*cos(c/2)+sin(a/2)*sin(b/2)*sin(c/2)   |         (w)
         |   sin(a/2)*cos(b/2)*cos(c/2)-cos(a/2)*sin(b/2)*sin(c/2)    |         (x)
     q=|   cos(a/2)*sin(b/2)*cos(c/2)+sin(a/2)*cos(b/2)*sin(c/2)   |         (y)
         |   cos(a/2)*cos(b/2)*sin(c/2)-sin(a/2)*sin(b/2)*cos(c/2)    |         (z)

  • 相关阅读:
    深入理解JS中的变量及变量作用域
    浏览器加载、解析、渲染的过程
    gerrit和git
    宽高等比缩放
    常见的网站性能优化手段
    JS实现数组去重(重复的元素只保留一个)
    重构与回流
    APP开放接口API安全性——Token令牌Sign签名的设计与实现
    索引原理-btree索引与hash索引的区别
    从四个维度谈谈如何做好团队管理
  • 原文地址:https://www.cnblogs.com/hyamw/p/454000.html
Copyright © 2011-2022 走看看