Game Development Reference
allow us to create quaternions for different kinds of values we have, we can create qua-
ternions from Euler angles or from an axis and an angle or from a rotation matrix.
A good quaternion class should provide the following functions:
quaternion CreateFromRotationMatrix(const matrix& m)
Given a rotation matrix, this function will create a quaternion and return it.
quaternion CreateFromAxisAngle(const vector3& axis, float angle)
Given an axis v , create a quaternion that will rotate by some angle in radians.
quaternion CreateFromDirection(const vector3& axis)
Given a direction vector v, it will return a quaternion rotated towards the axis (the angle is
calculated against the standard basis).
quaternion CreateFromEuler(float yaw, float pitch, float roll)
Given a set of Euler Angles, yaw, pitch and roll (in radians), it will return a quaternion ro-
tated towards those angles. Euler angles are susceptible to gimbal lock, even when using
them to create quaternions. That does not mean this function is useless, we just need to be
mindful of the angles we pass in to prevent losing a degree of freedom.
Quaternions, like rotation matrices may be concatenated to create complex rotations.
quaternion x_rotation = quaternion::CreateFromAxisAngle(vector3::UnitX, Pi/4);
quaternion y_rotation = quaternion::CreateFromAxisAngle(vector3::UnitY, Pi/4);
quaternion z_rotation = quaternion::CreateFromAxisAngle(vector3::UnitZ, Pi/4);
quaternion rotation = x_rotation * y_rotation * z_rotation;
convert this quaternion into a rotation matrix that we can then use to transform any vertex.
matrix result = matrix::CreateFromQuaternion(rotation);
Another useful trait of quaternions is the ability to perform spherical linear interpolation,
slerp . A slerp allows us to perform smooth animation of rotations in 3D, essentially, inter-
polate from one quaternion to another. A slerp will give us the shortest and straightest path
between the starting quaternion and the ending quaternion.