# Rotations using quaternions

← Back: Multiplication of quaternions |
Overview: Quaternions |
Next: Composition of rotations → |

Usually rotations are defined by 3 angles, either Euler or Roll-Pitch-Yaw angles. So three successive rotations around three different axes lead to a combined rotation, that can be described by a single rotation matrix. Such a combined rotation is equal to a rotation around a certain axis in three-dimensional space about a certain angle. This is shown in the figure on the right. The vector is rotated such that it results in . This could be done by rotating the vector around the z-, the y- and the x-axis successively or by just rotating it around the rotation axis by .

### Mathematical description

First the rotation axis has to be defined by a unit vector. So it holds:

Then a rotation about by the angle can be represented by a quaternion like follows:

So the scalar part is defined as the cosine of and the vector part consisting of , and equals multiplied by the sine of .

That the resulting quaternion is a unit quaternion can be proven as follows:

So if a quaternion is given, the angle and the rotation axis can be computed as follows:

### Mathematical solving of a quaternion rotation

Assume a quaternion describing a rotation and a vector that should be rotated. To be able to apply the rotation, has to be presented as a quaternion. So the pure quaternion is defined with scalar part equal to 0 and as vector part:

The same is done for the resulting vector of the rotation:

The rotation is then applied by the following quaternion multiplication:

Following the rules for quaternion multiplication, this equation leads to

This shows, that the result of the multiplication is again a pure quaternion describing the rotated vector . The corresponding rotation matrix describing the rotation obtained by the quaternion can easily be computed using the equation for .

How several rotations can be composed in one quaternion is explained in the next article Composition of rotations.

Example: Quaternion rotation and rotation matrices
An object is shifted 1 unit on the x-axis. So its position vector and the corresponding quaternion are defined as follows: Assume the object or the vector, respectively, is rotated corresponding to the Roll-Pitch-Yaw notation like shown in the figure on the right. So the first rotation is applied around the z-axis by a yaw angle of (shown in blue). Then it is rotated around the y-axis by a pitch angle of (shown in green). The roll angle is defined as around the x-axis (shown in red). Multiplying the three rotation matrices leads to the following combined rotation matrix: The resulting vector of the rotation, that is also shown in the figure on the right, then is: Regarding the figure it can easily be seen, that a rotation around the diagonal of the xz-plane by results in the same vector . The corresponding diagonal vector of length is . So this rotation can be described by a quaternion as follows: Using the quaternion rotation equation and solving it then leads to: So the rotation matrices for the rotation done following the Roll-Pitch-Yaw rule and the corresponding rotation using a quaternion lead to the same rotation matrices and and so to the same vector . The applet at the bottom of this page can be used to reconstruct the example situation and to test other combinations of roll, pitch and yaw angle and the corresponding quaternions. |

### Applet

The following three-dimensional applet helps you to understand the relation between Roll-Pitch-Yaw angles and a quaternion. The initial position of the object can be set using the sliders for x, y and z. Then the object can be rotated by defining the roll, pitch and yaw angles. The most intuitive way is to start with the yaw angle, because this one is applied first. Then the object is rotated aroud the y-axis by the pitch angle followed by a rotation around the x-axis by the roll angle. The quaternion describing the same rotation is shown dynamically and the corresponding angle and the rotation axis are presented. The rotation axis and the rotational path are visualized on the left side. After pressing the *Show Quaternion Rotation* button, the rotation of the object around gets animated.