• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            3D中的方位和角位移的C++實現(3)

            新建網頁 1

             
            cRotationMatrix類的目的就是處理非常特殊的(也是極其常用的)物體和慣性坐標空間之間的旋轉。這個矩陣類不是一般的變換類,我們假定這個類只包含旋轉,因此,它是正交的。換句話說,該矩陣表達的是方位,而不是角位移。當你創建這樣的矩陣時,不必指定變換的方向(物體坐標空間到慣性坐標空間或是慣性坐標空間到物體坐標空間)。變換的方向在實際執行變換時指定,每個方向對應一個函數。

            RotationMatrix.h:

                #ifndef ROTATION_MATRIX_H
               
            #define ROTATION_MATRIX_H
               
               
            class cVector3;
               
            class cEulerAngles;
               
            class cQuaternion;
               
               
            //---------------------------------------------------------------------------
                // Implement a simple 3x3 matrix that is used for ROTATION ONLY.  The
                // matrix is assumed to be orthogonal.  The direction of transformation
                // is specified at the time of transformation.
                //---------------------------------------------------------------------------
               
            class cRotationMatrix
                {
               
            public:
                    
            float    m11, m12, m13;
                    
            float    m21, m22, m23;
                    
            float    m31, m32, m33;    
               
               
            public:
                    
            void identity();
               
                    
            // setup the matrix with a specified orientation
               
                void setup(const cEulerAngles& orientation);
               
                    
            // setup the matrix from a quaternion, assuming the quaternion performs the
                    // rotation in the specified direction of transformation.
               
                void from_inertial_to_object_quat(const cQuaternion& q);
                    
            void from_object_to_inertial_quat(const cQuaternion& q);
               
                    
            // perform rotations
               
                cVector3 inertial_to_object(const cVector3& v) const;
                    cVector3 object_to_inertial(
            const cVector3& v) const;
                };
               
               
            #endif

            因為cRotationMatrix類很簡單,因此也非常容易使用。首先,用歐拉角或四元數設置矩陣。如果使用四元數,還必須指明該四元數代表哪種角位移。一旦創建了矩陣,就能用inertial_to_object()object_to_inertial()函數執行旋轉。

            cRotationMatrix.cpp

                #include "vector3.h"
                #include "RotationMatrix.h"
                #include "MathUtil.h"
                #include "Quaternion.h"
                #include "EulerAngles.h"
               
               
            /////////////////////////////////////////////////////////////////////////////
               
            //
                // MATRIX ORGANIZATION
                //
                // A user of this class should rarely care how the matrix is organized.
                // However, it is of course important that internally we keep everything
                // straight.
                //
                // The matrix is assumed to be a rotation matrix only, and therefore
                // orthoganal.  The "forward" direction of transformation (if that really
                // even applies in this case) will be from inertial to object space.
                // To perform an object->inertial rotation, we will multiply by the
                // transpose.
                //
                // In other words:
                //
                // Inertial to object:
                //
                //                  | m11 m12 m13 |
                //     [ ix iy iz ] | m21 m22 m23 | = [ ox oy oz ]
                //                  | m31 m32 m33 |
                //
                // Object to inertial:
                //
                //                  | m11 m21 m31 |
                //     [ ox oy oz ] | m12 m22 m32 | = [ ix iy iz ]
                //                  | m13 m23 m33 |
                //
                // Or, using column vector notation:
                //
                // Inertial to object:
                //
                //     | m11 m21 m31 | | ix |    | ox |
                //     | m12 m22 m32 | | iy | = | oy |
                //     | m13 m23 m33 | | iz |    | oz |
                //
                // Object to inertial:
                //
                //     | m11 m12 m13 | | ox |    | ix |
                //     | m21 m22 m23 | | oy | = | iy |
                //     | m31 m32 m33 | | oz |    | iz |
                //
               
            /////////////////////////////////////////////////////////////////////////////
               

               
            //---------------------------------------------------------------------------
                // Set the matrix to the identity matrix
                //---------------------------------------------------------------------------
               
            void cRotationMatrix::identity()
                {
                    m11 = 1.0f;    m12 = 0.0f; m13 = 0.0f;
                    m21 = 0.0f;    m22 = 1.0f; m23 = 0.0f;
                    m31 = 0.0f;    m32 = 0.0f; m33 = 1.0f;
                }
               
               
            //-----------------------------------------------------------------------------------------------------
                // Setup the matrix with the specified orientation
                //
                //     | cosh * cosb + sinh * sinp * sinb      -cosh * sinb + sinh * sinp * cosb         sinh * cosp |
                // M = | sinb * cosp                            cosb * cosp                                 -sinp         |
                //       | -sinh * cosb + cosh * sinp * sinb        sinb * sinh + cosh * sinp * cosb        cosh * cosp  |
                //-----------------------------------------------------------------------------------------------------
               
            void cRotationMatrix::setup(const cEulerAngles& orientation)
                {
                    
            // Fetch sine and cosine of angles
               

                    
            float sh,ch, sp,cp, sb,cb;
               
                    sin_cos(&sh, &ch, orientation.heading);
                    sin_cos(&sp, &cp, orientation.pitch);
                    sin_cos(&sb, &cb, orientation.bank);
               
                    
            // Fill in the matrix elements
               

                    m11 = ch * cb + sh * sp * sb;
                    m12 = -ch * sb + sh * sp * cb;
                    m13 = sh * cp;
               
                    m21 = sb * cp;
                    m22 = cb * cp;
                    m23 = -sp;
               
                    m31 = -sh * cb + ch * sp * sb;
                    m32 = sb * sh + ch * sp * cb;
                    m33 = ch * cp; 
                }
               
               
            //-----------------------------------------------------------------------------------------
                // Setup the matrix, given a quaternion that performs an inertial->object rotation.
                //
                //         | 1 - 2(y^2 + z^2)        2(xy + wz)            2(xz - wy)         |
                // M = | 2(xy - wz)                1 - 2(x^2 + z^2)    2(yz + wx)         |
                //         | 2(xz + wy)                2(yz - wx)            1 - 2(x^2 + y^2) |
                //-----------------------------------------------------------------------------------------
               
            void cRotationMatrix::from_inertial_to_object_quat(const cQuaternion& q)
                {
                    
            // Fill in the matrix elements.  This could possibly be optimized since there are 
                    // many common subexpressions. We'll leave that up to the compiler
               

                    m11 = 1.0f - 2.0f * (q.y * q.y + q.z * q.z);
                    m12 = 2.0f * (q.x * q.y + q.w * q.z);
                    m13 = 2.0f * (q.x * q.z - q.w * q.y);
               
                    m21 = 2.0f * (q.x * q.y - q.w * q.z);
                    m22 = 1.0f - 2.0f * (q.x * q.x + q.z * q.z);
                    m23 = 2.0f * (q.y * q.z + q.w * q.x);
               
                    m31 = 2.0f * (q.x * q.z + q.w * q.y);
                    m32 = 2.0f * (q.y * q.z - q.w * q.x);
                    m33 = 1.0f - 2.0f * (q.x * q.x + q.y * q.y);
                }
               
               
            //-----------------------------------------------------------------------------------------
                // Setup the matrix, given a quaternion that performs an object->inertial rotation.
                //
                //         | 1 - 2(y^2 + z^2)        2(xy - wz)            2(xz + wy)         |
                // M = | 2(xy + wz)                1 - 2(x^2 + z^2)    2(yz - wx)         |
                //         | 2(xz - wy)                2(yz + wx)            1 - 2(x^2 + y^2) |
                //-----------------------------------------------------------------------------------------
               
            void cRotationMatrix::from_object_to_inertial_quat(const cQuaternion& q)
                {
                    
            // Fill in the matrix elements.  This could possibly be optimized since there are 
                    // many common subexpressions. We'll leave that up to the compiler
               

                    m11 = 1.0f - 2.0f * (q.y * q.y + q.z * q.z);
                    m12 = 2.0f * (q.x * q.y - q.w * q.z);
                    m13 = 2.0f * (q.x * q.z + q.w * q.y);
               
                    m21 = 2.0f * (q.x * q.y + q.w * q.z);
                    m22 = 1.0f - 2.0f * (q.x * q.x + q.z * q.z);
                    m23 = 2.0f * (q.y * q.z - q.w * q.x);
               
                    m31 = 2.0f * (q.x * q.z - q.w * q.y);
                    m32 = 2.0f * (q.y * q.z + q.w * q.x);
                    m33 = 1.0f - 2.0f * (q.x * q.x + q.y * q.y);
                }
               
               
            //---------------------------------------------------------------------------
                // Rotate a vector from inertial to object space
                //---------------------------------------------------------------------------
               
            cVector3 cRotationMatrix::inertial_to_object(const cVector3& v) const
                {
                    
            // perform the matrix multiplication in the "standard" way
               
                return cVector3(m11 * v.x + m21 * v.y + m31 * v.z,
                                    m12 * v.x + m22 * v.y + m32 * v.z,
                                    m13 * v.x + m23 * v.y + m33 * v.z);
                }
               
               
            //---------------------------------------------------------------------------
                // Rotate a vector from object to inertial space
                //---------------------------------------------------------------------------
               
            cVector3 cRotationMatrix::object_to_inertial(const cVector3& v) const
                {
                    
            // Multiply by the transpose
               
                return cVector3(m11 * v.x + m12 * v.y + m13 * v.z,
                                    m21 * v.x + m22 * v.y + m23 * v.z,
                                    m31 * v.x + m32 * v.y + m33 * v.z);
                }

            posted on 2008-02-19 09:49 lovedday 閱讀(502) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            国产精品岛国久久久久| 精品久久久无码中文字幕天天| 久久国产成人精品国产成人亚洲| 久久精品国产亚洲沈樵| 久久最新精品国产| 青青青青久久精品国产h久久精品五福影院1421 | 99久久精品免费看国产一区二区三区| 国产精品无码久久久久久| 国产精品久久成人影院| 99热精品久久只有精品| 久久精品国产乱子伦| 久久综合九色综合97_久久久| 欧美精品九九99久久在观看| 国产精品国色综合久久| 亚洲国产精品无码久久98| 国产精品美女久久久免费| 久久综合色老色| 伊人久久大香线蕉影院95| 久久精品无码专区免费青青| 国产精久久一区二区三区| 久久久久一区二区三区| 日产精品99久久久久久| 久久中文字幕人妻丝袜| 亚洲国产美女精品久久久久∴| 一本大道久久香蕉成人网| 综合久久一区二区三区 | 久久99精品久久久久久野外| 久久精品人人做人人爽97| 久久国产精品成人免费| 久久电影网一区| 久久国产视频99电影| 国产精品丝袜久久久久久不卡| 久久中文娱乐网| 日韩精品久久久久久久电影| 亚洲精品国产字幕久久不卡| 久久成人精品视频| 一本色道久久综合| 精品久久久久久无码中文字幕一区| 日本久久久精品中文字幕| 麻豆亚洲AV永久无码精品久久 | 久久国产精品无码一区二区三区|