Main Page | See live article | Alphabetical index

# Quaternions and spatial rotation

Quaternions are used in computer graphics and related fields because they allow for a compact representations of rotations in 3D space. This will be explained in this article.

## Introduction

Every quaternion z = a + bi + cj + dk can be viewed as a sum a + u of a real number a (called the "real part" of the quaternion) and a 3-vector u = (b, c, d) = bi + cj + dk in R3 (called the "imaginary part"). In this view, quaternions are "mixed sums" of scalars and 3-vectors and the quaternions i, j and k correspond to the unit vectors i, j and k.

Two such quaternions are added by adding the real parts and the imaginary parts separately:

(a + u) + (b + v) = (a + b) + (u + v)
The multiplication of quaternions translates into the following rule:
(a + u) (b + v) = (ab - <u,v>) + (av + bu + u×v)
Here, <u,v> denotes the scalar product and u×v the vector product of u and v.

This formula shows that two quaternions z and w commute, i.e. zw = wz, if and only if their imaginary parts are real multiples of each other (because in this case the vector product of these imaginary parts will commute).

It is well known that the vector product is related to rotation in space. The goal then is to find a formula which expresses rotation in 3-d space using quaternion multiplication, similar to the formula for a rotation in 2-d using complex multiplication:

f(w) = zw
where z = exp(iα) is used for rotation by an angle α.

The formula in 3-d cannot be a simple multiplication with a quaternion, because multiplying a vector with a non-trivial quaternion yields a result with non-zero real part, and thus not a vector. Rotating a vector should yield a vector however.

It turns out that we can cancel the real part if we multiply by a quaternion from one side and with its inverse from the other side. Let z = a + u be a non-zero quaternion, and consider the function

f(v) = z v z-1
where z-1 is the multiplicative inverse of z and v is a vector, considered as a quaternion with zero real part. The function f is known as conjugation by z. Note that the real part of f(v) is zero, because in general zw and wz have the same real part for any quaternions z and w, and so
Re(z v z-1) = Re(v z-1 z) = Re(v 1) = 0.
Furthermore, f is a R-linear and we have f(v) = v if and only if v and the imaginary part u of z are real multiples of each other (because f(v) = v means v z = z v). Hence f is a rotation whose axis of rotation passes through the origin and is given by the real multiples of v.

Note that conjugation with z is the same as conjugation with rz for any real number r. We can thus restrict our attention to the quaternions of absolute value 1, the so-called unit-quaternions. (The absolute value |z| of the quaternions z = a + v is defined as the square root of a2 + ||v||2. It is multiplicative: |zw| = |z| |w|.) Inverting unit quaternions is especially easy: if |z| = 1, then z-1 = z* (the conjugate z* of the quaternion z = a + v is defined as z* = a - v) and this makes our rotation formula even easier.

It turns out that the angle of rotation α is also easy to read off if we are dealing with a unit quaternion z = a + v: we have cos(α/2) = a. To summarize:

A rotation in 3-d with axis v and angle α can be represented as conjugation with the unit quaternion z = cos(α/2) + sin(&alpha/2)v/||v|| (or with any real multiple of z).

The composition of two rotations corresponds to quaternion multiplication: if the rotation f is represented by conjugation with the quaternion z and the rotation g is represented by conjugation with w, then the composition fog is represented by conjugation with zw.

If one wishes to rotate about an axis that doesn't pass through the origin, then one first translates the vectors into the origin, conjugates, and translates back.

## An example

The length of u is √3, the half angle is π/3 with cosine 1/2 and sine (√3)/2. We are therefore dealing with a conjugation by the unit quaternion z = (1 + i + j + k)/2.

Concretely:

f(ai + bj + ck) = z (ai + bj + ck) z*
which using the ordinary rules for quaternion arithmetic can be simplified to
ci + aj + bk
as expected.

## Quaternions vs. other representations of rotations

In computer games and other applications, one is often interested in "smooth rotations", meaning that the scene should slowly rotate and not in a single step. This can be accomplished by choosing a curve in the quaternions, with one endpoint being the identity transformation 1 and the other being the intended total rotation. This is more problematic with other representations of rotations.

When composing several rotations on a computer, rounding errors necessarily accumulate. A quaternion that's slightly off still represents a rotation -- a matrix that's slightly off need not be orthogonal anymore and therefore need not represent a rotation at all. It is hard to turn such a matrix back into a proper orthogonal one.