## 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, is a polynomial, and is a polynomial, and is a polynomial, and is a polynomial.

The general form of a polynomial is A complex number is very similar to a polynomial, but with an additional rule that the new element, , has the property that . So, any integer power of greater than 1 is in . Instead of , you could just write .

The general form of a complex number is .

Quaternions can be generated by extending the real numbers with 3 new elements , , and , such that . It’s important to note that multiplication with , ,and is not commutative in general. You can show that , but , for example. As a result of these rules, any quaternion can be written as , where , , , and are real numbers. You might notice that when , you have the complex numbers. Another way to write quaternions is , where is a real number, and 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: using multiplication of the basis elements, we can derive multiplication of general quaternions.

let  then     and by rearanging, you can get: where     so ### 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. The inverse of a quaternion is because and for a unit quaternion, this simplifies to And to compose roations, you can conjugate twice 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 , and angle , the quaternion is: 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.