Quaternions part 1

posted by harrison on August 30, 2012

For a while I’ve been thinking about quaternions. Here’s an introduction to the mathematical basis of quaternions for representing rotations in 3d.

Why would you use a quaternion?

Quaternions are a convenient way to represent rotations in 3 dimensional space. Unit quaternions (quaternions of length 1) are used for this purpose. The main advantage they have over Euler angles is that they can be interpolated without looking awful. You can compose rotations by multiplying quaternions, much like with rotation matricies, but a unit quaternions is always a rotation, while a 3×3 matrix could be a number of other types of (possibly undesirable) transformations. This property means that you can multiply or interpolate a bunch of quaternions, and even if there is some numerical inaccuracy, you can just normalize the result and you still have a rotation.

There are other uses outside of game development and other 3d math, but they are rather rare. They have uses in relativity, and dual quaternions can be used to represent spatial displacement.

What is a quaternion?

Much like complex numbers or polynomials, quaternions are real numbers extended with additional elements.

Polynomials can be generated by taking the real numbers, and adding a new element ‘x’ such that multiplication and addition are both still associative and commititave. For example, 5.3 is a polynomial, and 5.3x is a polynomial, and \pi x + 1 is a polynomial, and x*3*x*x*4 + x*x = 12x^3 + x^2 is a polynomial.

The general form of a polynomial is \Sigma^{\infty}_{n=0}a_nx^n

A complex number is very similar to a polynomial, but with an additional rule that the new element, i, has the property thati^2 = -1. So, any integer power of i greater than 1 is in (1, -1, i -i ). Instead of 2 + 3i + 4i^2 + \pi i^5, you could just write 2 + 3i - 4 + \pi i = -2 + (3+\pi)i.

The general form of a complex number is a + bi.

Quaternions can be generated by extending the real numbers with 3 new elements i, j, and k, such that i^2 = j^2 = k^2 = ijk = -1. It’s important to note that multiplication with i, j,and k is not commutative in general. You can show that ij = k, but ji = -k, for example. As a result of these rules, any quaternion can be written as a + bi + cj + dk, where a, b, c, and d are real numbers. You might notice that when c = d = 0, you have the complex numbers. Another way to write quaternions is a + v, where a is a real number, and v is a 3 dimensional vector. This alternate representation is convenient for some operations.

How do you multiply quaternions?

Multiplication can be derived right from the definition. I’m not going to go into detail in this post, but the basis elements (1, i, j, k) form a multiplicative group with the following multiplication table:

    \[ \begin{tabular}{|c||c|c|c|c|} \hline * & 1 & i & j & k \\ \hline \hline 1 & 1 & i & j & k \\ \hline i & i & -1 & k & -j \\ \hline j & j & -k & -1 & i \\ \hline k & k & j & -i & -1 \\ \hline \end{tabular} \]

using multiplication of the basis elements, we can derive multiplication of general quaternions.


    \[q = a + bi + cj + dk = a + v \]

    \[q' = a' + b' i + c' j + d' k = a' + v' \]



    \[q q' = (a + bi + cj + dk)q' = a q' + bi q' + cj q' + dk q'  \]

    \[a q' = a a' + a b' i + a c' j + a d' k \]

    \[bi q' = b a' i + b b' i^2 + b c' i j + b d' i k = b a' i - b b' + b c' k - b d' j \]

    \[cj q' = c a' j + c b' j i + c c' j^2 + c d' j k = c a' j - c b' k - c c' + c d' i\]

    \[dk q' = d a' k + d b' k i + d c' k j + d d' k^2 = d a' k + d b' j - d c' i - d d' \]

and by rearanging, you can get:

    \[q q' = A + B + C + D + E \]


    \[A = a a'\]

    \[B = -b b' - c c' -d d' = -v \cdot v' \]

    \[C = a b' i + a c' j + a d' k = a v' \]

    \[D = a' bi + a' c j + a' d k = a' v \]

    \[E = c d' i - d c' i + d b' j - b d' j + b c' k - c b' k = v \times v' \]


    \[q q' = a a' - v \cdot v' + a v' + a' v + v \times v' \]

How do you apply the rotation?

Say you have a vector u that you want to rotate by a unit quaternion q. You can do it by treating the vector as a quaternion, and performing an operation called conjugation.

    \[u' = quq^{-1}\]

The inverse of a quaternion is

    \[q^{-1} = \frac{a-v}{a^2 + v \cdot v}\]


    \[qq^{-1} = \frac{a^2 + v \cdot v - av + av - v \times v}{a^2 + v \cdot v} = \frac{a^2 + v \cdot v}{a^2 + v \cdot v} = 1\]

and for a unit quaternion, this simplifies to

    \[q^{-1} = a - v \]

And to compose roations, you can conjugate twice

    \[u' = rquq^{-1}r^{-1} = (rq)u(rq)^{-1}\]

so composing rotations is the same as multiplying quaternions.

How do you construct a quaternion from another representation?

If you have an axis-angle representation, with axis v, and angle \theta, the quaternion is:

    \[cos(\frac{\theta}{2}) + sin(\frac{\theta}{2})\hat{v}\]

you can also go backwards, and convert from quaternion to axis-angle representation.

This post covered multiplication, inverse, conjugation, and axis-angle representation, so it should be enough math to make a crude quaternion library, but you really need the Slerp function for it to be worth the effort. In another post, I will explain why conjugation is rotation, and how the Slerp function works.

part 2 is up now.

Comments are closed.

Twisted Oak Studios offers consulting and development on high-tech interactive projects. Check out our portfolio, or Give us a shout if you have anything you think some really rad engineers should help you with.


More interesting posts (27 of 33 articles)

Or check out our Portfolio.