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

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.

主站蜘蛛池模板: 伊金霍洛旗| 阜平县| 门头沟区| 察雅县| 衡阳市| 邵东县| 青岛市| 屏南县| 罗山县| 桃园市| 肇庆市| 新巴尔虎右旗| 靖江市| 东乡族自治县| 健康| 景德镇市| 诏安县| 惠来县| 普格县| 荥经县| 两当县| 洛隆县| 义马市| 上栗县| 维西| 紫金县| 广水市| 青海省| 吉首市| 昌吉市| 寿阳县| 拉萨市| 郓城县| 伊宁县| 兴化市| 象州县| 寿光市| 兴宁市| 阿克苏市| 北碚区| 扎囊县|