# Representing Rotations in Quaternion Arithmetic

Quaternions crop up a lot in game development, since they are an efficient way to store rotations in 3-space. This article attempts to serve as a mathematical introduction to quaternions, and explains how and why we choose to use them to represent 3D rotations.

## Basic Quaternion Arithmetic

Quaternions form what is called a non-commutative division ring. This space can be visualised in a similar manner to complex numbers. Just as the complex numbers can be represented by a two-dimensional co-ordinate on the complex plane, quaternions can be represented by a co-ordinate in 4-D space. This is written:

The numbers `$\boldsymbol{i}$`

, `$\boldsymbol{j}$`

and `$\boldsymbol{k}$`

are related to the real numbers and each other by the following equalities:

Quaternions can be viewed as a vector space over the real numbers. In this way they are given the notation:

When adding or subtracting quaternions you perform the same operation as for vectors and matrices: add and subtract component-wise. As with many other vector spaces, the quaternions come equipped with a norm, which is function (usually regarded as a “length”) that returns a single real number given a quaternion. It is usually defined as:

A non-commutative division ring has more structure than a vector space, namely a non-commutative multiplication operator. Without worrying too much about the exact definitions of these words (those interested can pick up the nearest undergraduate text on algebra) this basically means they form a special kind of number system. The word “non-commutative” means that multiplication is not symmetric, in other words:

We can define quaternion multiplication using the second vector notation above and the normal vector dot and cross product as:

This is entirely equivalent to multiplying out the various components of i, j and k using the very first notation given earlier. Try it if you don’t believe me! :)

## Conjugates And Inversion

Now that we can add, subtract and multiply quaternions it would be useful to also be able to divide them. As a direct extension of the complex conjugate in complex arithmetic, the quaternion conjugate is defined as:

By multiplying this out using the definition of multiplication above, it can be shown that:

This equality allows us to construct the inverse of any quaternion:

We can use the above definitions to prove that:

## Exponentials, Logarithms And Powers

Since quaternions are really just numbers you would expect there to be a natural extension of our idea of exponentials and logarithms for real numbers. Well it just so happens that there is, and it is derived in a similar way to the extension for complex numbers. This section is aimed at the mathematically fearless and can be skipped if need be without causing confusion in later sections.

By considering the series expansion of the exponential, it can be shown that:

This leads to a very useful factorisation of a quaternion:

Using the identity for exponential above, we can write this as:

Note how similar this is to the `$r, \theta$`

factorisation of complex numbers.
Once we have this factorisation, we can trivially define the quaternion logarithm as:

We can then define quaternion powers in terms of the logarithm and exponential, namely:

## A Quaternion Rotation

For a rotation of `$\phi$`

about a unit axis `$\boldsymbol{w}$`

, we will define the representing quaternion `$q$`

to be:

Note that using this scheme that the norm of all quaternion rotations is 1 (i.e. they are all *unit* quaternions).
The converse is also true: any unit quaternion can be decomposed into this form (just like we did above for logarithms) and can therefor represent a rotation. It is therefore simple to adjust for numerical drift: simply renormalise the quaternion.
This takes far fewer operations than matrix orthonormalisation, which corrects numerical drift in rotation matrices.

To rotate a vector `$\boldsymbol{v}$`

an angle of `$\phi$`

around about an arbitrary unit axis `$\boldsymbol{w}$`

, you can use the formula:

Representing the same rotation by a quaternion `$q$`

, you can prove (go on, try it!) that this equivalent to:

This formula uses quaternion multiplication and conjugation as defined earlier. This equation is one of the reasons for encoding rotations in quaternions, since it greatly simplifies the mathematics of composing rotations. We can also describe composition of rotations:

This shows that multiplication of quaternions is equivalent to *composition* of rotations.
Just like composing rotations, we need to be careful about the order in which we multiply.
In the following notation, rotations are applied from right to left.

Similarly, inverting a quaternion (note that for unit quaternions this is just the conjugate directly) produces the inverse rotation. Later, we will show how to use quaternion powers to do partial rotations, or blend between them.

## Quaternion To Matrix Conversion

Although I hate to admit it, it is often necessary to have a quaternion rotation in matrix form, e.g. to load onto graphics hardware for hardware vertex transformations. In this case it useful to have a formula for converting a quaternion quickly into a rotation matrix. Using the scheme of pre-multiplication of matrices given earlier, one such formula for this is:

As I have had recently pointed out to me, applying a quaternion rotation to a vector directly takes a couple of operations more than computing the matrix and applying the matrix directly (41 vs 39 operations). The direct quaternion rotation uses fewer registers for the computation, but once the matrix has been computed only 15 operations are needed to apply it to another vector, so for rotating more than one vector it is much more efficient to use the above matrix form.

## SLERP

SLERP stands for spherical-linear interpolation and is one method of interpolating two rotations smoothly. It is called spherical-linear since the two quaternion rotations are interpolated uniformly along a geodesic in the surface of the 3-sphere.

The general idea behind interpolation is to have a variable *t* that ranges from 0 to 1, and use this to parameterise a smooth blend between quaternions `$a$`

and `$b$`

. Spherical linear interpolation uses the following equation:

Clearly *q* is *a* when *t* is 0, and *b* when *t* is 1. Note how we have been careful with our order of multiplication: quaternions are applied from right to left.
General quaternion powers were described earlier, but since *t* is a real number, and all rotation quaternions are of unit length, we can greatly simplify the power equation:

This nicely fits with our intuition that linearly interpolating between the identity transform and a rotation should be a linear change of angle.