- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 154字
- 2021-06-30 14:46:02
Length and squared length
Like vectors, the squared length of a quaternion is the same as the dot product of the quaternion with itself. The length of a quaternion is the square root of the square length:
- Implement the lenSq function in quat.cpp and declare the function in quat.h:
float lenSq(const quat& q) {
return q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
}
- Implement the len function in quat.cpp. Don't forget to add the function declaration to quat.h:
float len(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 0.0f;
}
return sqrtf(lenSq);
}
Quaternions that represent a rotation should always have a length of 1. In the next section, you will learn about unit quaternions, which always have a length of 1.
推薦閱讀