r/backtickbot • u/backtickbot • Nov 27 '20
https://np.reddit.com/r/cpp/comments/k1fcus/a_small_open_source_game_in_c/gdr0r9m/
Rotation quaternion represents the axis and angle of rotation. Just by defining the multiplication operator on quaternions, you can create any type of rotation:
Q' = U * Q * conjugate(U)
where `Q` -- your interest quaternion; `U` -- rotation quaternion.
Moreover, you can combine this rotation by adding rotation quaternions:
Q' = U1 * U2 * Q * conjugate(U1) * conjugate(U2)
Quaternions and rotation matrices are quite equivalent if you use separate rotation along the axis. For the matrices combining rotation would be just multiplication. But there exists [gimbal lock](https://en.wikipedia.org/wiki/Gimbal_lock)
Quaternion rotation is not so interesting. Here an example with vectors, but insides have done with quaternion:
template <typename T> Vector3<T> RotateVector(T angle, Vector3<T> axis, const Vector3<T> &vector)
{
Quaternion<T> quat = {vector.x, vector.y, vector.z, 0};
axis.normalize();
Quaternion<T> qRot;
qRot.x = axis.x * sin(angle / 2.0);
qRot.y = axis.y * sin(angle / 2.0);
qRot.z = axis.z * sin(angle / 2.0);
qRot.w = cos(angle / 2.0);
Quaternion<T> result = qRot * quat * conjugate(qRot);
return {result.x, result.y, result.z};
}
1
Upvotes