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

Unit quaternions

Quaternions can be normalized just like vectors. Normalized quaternions represent only a rotation and non-normalized quaternions introduce a skew. In the context of game animation, quaternions should be normalized to avoid adding a skew to the transform.

To normalize a quaternion, divide each component of the quaternion by its length. The resulting quaternion's length will be 1. This can be implemented as follows:

  1. Implement the normalize function in quat.cpp and declare it in quat.h:

    void normalize(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;

       }

       float i_len = 1.0f / sqrtf(lenSq);

       q.x *= i_len;

       q.y *= i_len;

       q.z *= i_len;

       q.w *= i_len;

    }

  2. Implement the normalized function in quat.cpp, and declare it in quat.h:

    quat normalized(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 il = 1.0f / sqrtf(lenSq); // il: inverse length

       return quat(q.x * il, q.y * il, q.z * il,q.w * il);

    }

There is a fast way of inverting any unit quaternion. In the next section, you will learn how to find the conjugate and inverse of a quaternion and their relationship when it comes to unit quaternions.

主站蜘蛛池模板: 夏河县| 中卫市| 全南县| 浦县| 离岛区| 错那县| 红安县| 湛江市| 北安市| 托里县| 安宁市| 丰县| 昌吉市| 辰溪县| 门头沟区| 延寿县| 嵊州市| 台南市| 观塘区| 金乡县| 牙克石市| 常德市| 日喀则市| 五大连池市| 房产| 图们市| 宁强县| 遵化市| 玉环县| 恩施市| 宁陵县| 沁源县| 玉田县| 左云县| 民丰县| 平武县| 垦利县| 漠河县| 五常市| 蒲城县| 玉溪市|