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

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.

主站蜘蛛池模板: 岑溪市| 同德县| 本溪| SHOW| 邯郸市| 宁海县| 光泽县| 进贤县| 上杭县| 广德县| 乌拉特前旗| 桂阳县| 修武县| 西平县| 满洲里市| 吕梁市| 诏安县| 车险| 塔河县| 普兰店市| 凤凰县| 三都| 文水县| 蒙山县| 留坝县| 西盟| 昌图县| 双牌县| 芒康县| 晋中市| 会理县| 长葛市| 从化市| 沅陵县| 茌平县| 贞丰县| 叶城县| 前郭尔| 五家渠市| 昆明市| 雷山县|