官术网_书友最值得收藏!

Conjugate and inverse

Games mostly use normalized quaternions, which comes in handy when inverting quaternions. The inverse of a normalized quaternion is its conjugate. The conjugate

of a quaternion flips its axis of rotation:

  1. Implement the conjugate function in quat.cpp and remember to declare the function in quat.h:

    quat conjugate(const quat& q) {

        return quat(

            -q.x,

            -q.y,

            -q.z,

             q.w

        );

    }

  2. The proper inverse of a quaternion is the conjugate divided by the squared length of the quaternion. Implement the quaternion inverse function in quat.cpp. Add the function declaration to quat.h:

    quat inverse(const quat& q) {

       float lenSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;

       if (lenSq < QUAT_EPSILON) {

          return quat();

       }

       float recip = 1.0f / lenSq;

       return quat(-q.x * recip,

                   -q.y * recip,

                   -q.z * recip,

                    q.w * recip

       );

    }

If you need to find out whether a quaternion is normalized or not, check the squared length. The squared length of a normalized quaternion is always 1. If a quaternion is normalized, its conjugate and inverse are the same. This means you can use the faster conjugate function, instead of the inverse function. In the next section, you will learn how to multiply two quaternions together.

主站蜘蛛池模板: 平塘县| 株洲市| 罗甸县| 清丰县| 鄂温| 华池县| 亚东县| 西昌市| 易门县| 永丰县| 邮箱| 繁昌县| 方山县| 长阳| 微山县| 广西| 萍乡市| 北安市| 申扎县| 军事| 自治县| 北安市| 勃利县| 松原市| 永年县| 东辽县| 彝良县| 玉门市| 内黄县| 原平市| 五原县| 玛纳斯县| 富蕴县| 永寿县| 清远市| 大姚县| 中江县| 屏南县| 琼结县| 偏关县| 句容市|