Quaternions
A quaternion (pronounced qwa-ter-ne-ion) is a complex-number like system which contains three imaginary components and one real component. Arguably, the most useful quaternions is a subset of all quaternions called unit quaternions (or versors), which can be used to describe a rotation in 3D space. This page focuses primarily on these unit quaternions.
Defining Equation And Identities
The basic form of a quaternion is:
where:
are real numbers
are the quaternion units
(which can be seen as similar to the complex number )
The multiplication rules for the quaternion units are:
From these rule above, you can determine some identities:
Multiply both sides by :
Using the rule that :
And thus:
The same process can be applied the other way around and also to and resulting in the following six identities:
These identities are used to simplify terms when applying the product rule to quaternion multiplication.
Quaternion Conjugates
All quaternions have a conjugate. The conjugate of a quaternion represents a rotation in the opposite direction. For example, if describes the orientation of frame B
relative to frame A
, then would describe the orientation of frame A
relative to frame B
. The conjugate of a quaternion is:
All you need to do is multiple all of the imaginary terms by . The conjugate can also be denoted by or . However is the preferred choice on this site.
The conjugate of a unit quaternion is the same as it’s inverse.
Pure/Vector Quaternions
A pure quaternion is a quaternion where . This results in a quaternion:
This three-valued quaternion also happens to be able to represent a vector in 3D space, and so it is also called a vector quaternion. A vector quaternion is used to represent either a vector or point in 3D space when you want to apply a quaternion rotation to it (more on this below).
Scalar/Vector Notation
You can split a quaternion into a scalar and a vector component. The represents the scalar part of the quaternions and the represents the vector part. You may see a quaternion written with an for the scalar part and a for the vector part.
Unit Quaternions
Unit quaternions are quaternions in which the coefficients have been normalized as follows:
Interestingly, all rotations in 3D space (which is called the 3D rotation group, or )` can be represented by the unit quaternions (which is a sub-set of all quaternions). In fact, you will rarely ever deal with a quaternion that is not a unit quaternion!
Representation In Software/Programming
In software, quaternions are typically described with a vector/array (in the programming sense of the word) in the following form:
in where the coefficients are stored in an array and the quaternion units are implied by position.
It is important to note that although it is common to represent a quaternion with vector-like syntax, it is definitely not a vector (in the mathematical sense of the word). For one, quaternion multiplication does not follow the dot or cross-product multiplication rules that vectors do.
Why Use Quaternions To Describe Rotations?
There are a number of different ways to describe rotations. These include:
- Axis-angle representation
- Euler angles (roll, pitch, yaw)
- Rotation matrices
- Quaternions
Why choose Quaternions? One reason is that Quaternions do not suffer from the gimbal lock that Euler angles do. This also is related to the fact that they are differentiable (i.e. very small changes in rotation cause very small changes in the quaternion values), which allows for smooth interpolation, important for many use cases including 3D animation. They are also more compact with only four numbers need to be stored than a 9 number rotation matrix. Quaternions are also easily converted into the angle-axis representation and back again.
Rotating A Vector
You can rotate a vector in 3D space to with:
Because the vector is squashed between the quaternion and it’s conjugate, this is sometimes referred to as the “sandwich product”. The multiplications can be done by the basic product rule, or more easily using the Hamilton product.
Lets assume we have the vector:
And we want to rotate it with the quaternion>
This quaternion just so happens to be a rotation of around the y-axis>
We turn the vector into a vector quaternion by adding a fourth value of 0 (which is the ):
Combine into:
To calculate the multiplication of two quaternions, you can use the Hamilton product. If you have the equation:
where each quaternion is composed in the following manner:
then using the Hamilton product, the values of each component in are:
You can also rotate a vector by a quaternion by decomposing the quaternion into it’s scalar part and it’s vector part :
and then using the following formula to calculate the rotated vector :
where:
is the vector cross-product
Rotation Matrix
You can calculate a 3x3 rotation matrix from a quaternion. This is useful if you want to express the rotation as a matrix instead of a quaternion, but comes at the expense of having to store 9 numbers rather than 4!
Combining Rotations
Rotations can be easily combined when using quaternions.
Given two quaternion rotations that are to be applied consecutively, and then , the total rotation is found with:
Remember that quaternion multiplication is not associative, so the ordering of and is important.
Some Useful Quaternions
Quaternion | Description |
---|---|
Identity quaternion, no rotation. | |
Rotation of 180 around X axis. | |
Rotation of 180 around Y axis. | |
Rotation of 180 around Z axis. | |
Rotation of 90 around X axis. | |
Rotation of 90 around Y axis. | |
Rotation of 90 around Z axis. | |
Rotation of -90 around X axis. | |
Rotation of -90 around Y axis. | |
Rotation of -90 around Z axis. |
Interpolation
SLERP
SLERP is shorthand for spherical linear interpolation. It is commonly used with quaternions to produce a smooth rotation of a 3D body from one orientation to another. Performing SLERP on two quaternions produces a smooth, continuous rotation with a constant angular velocity around a fixed rotation axis (the first quaternion describes the starting rotation, the second quaternion describes the end rotation).
SLERP does not work well when the two quaternions are too similar. In this case it is safer to just perform linear interpolation on all four values quaternion values independently (making sure to normalize the resulting quaternion before using it).
The following Python code example demonstrates the SLERP algorithm on a pair of quaternions 1:
https://en.wikipedia.org/wiki/Slerp has some code examples in Python and C++ that perform SLERP on quaternions.
Conversion From Axis-Angle Form To Quaternion
A rotation in axis-angle form:
can be converted into a quaternion with:
Conversion From Quaternion To Rotation Matrix
If you have a quaternion in the form:
Then the equivalent rotation matrix is: