- Hands-On C++ Game Animation Programming
- Gabor Szauer
- 247字
- 2021-06-30 14:46:02
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:
- 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;
}
- 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.
- Java程序設計實戰教程
- Objective-C Memory Management Essentials
- Rake Task Management Essentials
- Java Web程序設計
- TypeScript實戰指南
- 鋒利的SQL(第2版)
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- Linux Device Drivers Development
- 自然語言處理Python進階
- Learning Laravel's Eloquent
- Mastering Apache Storm
- Elasticsearch搜索引擎構建入門與實戰
- 青少年Python趣味編程
- Python AI游戲編程入門:基于Pygame和PyTorch
- Python程序設計案例教程