Simon's Graphics Blog

Work log for ideas and hobby projects.

Representing Rotations In Quaternion Arithmetic

with 10 comments

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:

latex

The numbers i, j and k are related to the real numbers and each other by the following equalities:

latex

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

latex

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:

latex

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 [1]) this basically means they form a special kind of number system. The word “non-commutative” means that multiplication is not symmetric, in other words:

latex

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

latex

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:

latex

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

latex

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

latex

We can use the above definitions to prove that:

latex

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:

latex

latex

This leads to a very useful factorisation of a quaternion:

latex

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

latex

Note how similar this is to the r, θ factorisation of complex numbers. Once we have this factorisation, we can trivially define the quaternion logarithm as:

latex

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

latex

A Quaternion Rotation

For a rotation of φ about a unit axis w, we will define the representing quaternion q to be:

latex

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 v an angle of φ around about an arbitrary unit axis w, you can use the formula:

latex

Representing the same rotation by a quaternion q, you can prove (go on, try it!) that this equivalent to:

latex

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:

latex

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:

latex

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:

latex

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:

latex

This nicely fits with our intuition that linearly interpolating between the identity transform and a rotation should be a linear change of angle. In general spherical linear interpolation is not particularly useful; for example, it is too jerky for interpolating camera movements since there is an instant change in camera velocity at the interpolation points. However it is a simple introduction into interpolation and more useful interpolation methods such as…

Spherical-Cubic Interpolation

This scheme is the quaternion equivalent of Bezier or Hermite cubic curves. The idea behind it is to specify not only the start and end rotations, but also the rotational speeds at these points. This allows you to pass smoothly through your interpolation points by specifying the same leaving speed as your arrival speed. Let’s use the following notation for spherical linear interpolation (SLERP):

latex

And now we define spherical-cubic interpolation as:

latex

As before a and b are the start and end quaternions, with the curve defined by the quadrilateral a-p-q-b, and the path touches the midpoint of p-q when t is 0.5.

Using a small amount of quaternion calculus you can compute the differential at the start and end points. These are:

latex

At first glance it would seem that ensuring continuity in the differential at interpolation points would be a nightmare, but the equations simplify to a nice result. Consider the following:

latex

Then it can be shown that this function has continuous differentials at the interpolation points if:

latex

References

[1] Serge Lang – Algebra, Second Edition (1984) – Addison-Welsey Publishing Company Inc.

Written by Simon Brown

May 1st, 2002 at 8:00 pm

Posted in Quaternions

10 Responses to 'Representing Rotations In Quaternion Arithmetic'

Subscribe to comments with RSS or TrackBack to 'Representing Rotations In Quaternion Arithmetic'.

  1. Good post, adding it to my bookmarks!

    psp spiele

    28 Feb 09 at 9:14 pm

  2. good post only q*=(w,-v)=(w,-x,-y,-z);

    Arsham Atonyan

    23 Aug 09 at 8:39 am

  3. When you explaion rotation, you used “p”, WHAT’s “p” ??? Thank you

  4. In this case, p is just another quaternion rotation.

    Simon Brown

    8 Dec 09 at 5:02 pm

  5. Second equation from the top, should be: i^2 = j^2 = k^2 = -1

    Dan

    12 Dec 09 at 7:56 pm

  6. Thanks, also fixed!

    Simon Brown

    14 Dec 09 at 10:21 am

  7. When I look at this posting, there are a number of places where it says Invalid Equation, where I would expect a graphic of an equation. Not a very encouraging sign!

    Daniel

    25 Aug 10 at 8:49 pm

  8. Thanks for letting me know, I’ve fixed the offending TeX syntax!

    Simon Brown

    27 Aug 10 at 9:48 am

  9. I think your definition of the conjugate is wrong.
    You state that for q=(w,x,y,z) the conjugate q* is defined as q*=(x,-y,-z,-w).
    The correct definition is q*=(w,-x,-y,-z).

    Aside from that… great article ;-)

    Marc

    13 May 11 at 12:12 am

  10. Thanks for reporting, that’s been broken for years! Fixed.

    Simon Brown

    13 May 11 at 10:50 am

Leave a Reply