GEOMETRY
Quaternions
Date Published: | |
Last Modified: |
Overview<
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:
q=w+xi+yj+zk
where:
w,x,y,z are real numbers
i,j,k are the quaternion units
(which can be seen as similar to the complex number i)
The multiplication rules for the quaternion units are:
i2=j2=k2=ijk=−1
From these rule above, you can determine some identities:
ijk=−1
Multiply both sides by i:
i2jk=−i
Using the rule that i2=−1:
−jk=−i
And thus:
i=jk
The same process can be applied the other way around and also to j and k resulting in the following six identities:
i=jkj=−ikk=ij−i=kj−j=−ki−k=ji
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 q
describes the orientation of frame B
relative to frame A
, then ˉq
would describe the orientation of frame A
relative to frame B
. The conjugate of a quaternion q
is:
ˉq=w−xi−yj−zk
All you need to do is multiple all of the imaginary terms by −1
. The conjugate can also be denoted by q∗
or qT
. However ˉq
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 w=0
. This results in a quaternion:
q=xi+yj+zk
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 w
represents the scalar part of the quaternions and the x,y,z
represents the vector part. You may see a quaternion written with an w
for the scalar part and a →r
for the vector part.
q=(w,→r)
Unit Quaternions
Unit quaternions are quaternions in which the coefficients have been normalized as follows:
w2+x2+y2+z2=1
Interestingly, all rotations in 3D space (which is called the 3D rotation group, or SO(3))
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:
q=[wxyz]
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 v
in 3D space to v′
with:
[0v′]=q[0v]q′
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:
v=[100]
And we want to rotate it with the quaternion>
q=[√0.50√0.50]]
This quaternion just so happens to be a rotation of 90
around the y-axis>
We turn the vector into a vector quaternion by adding a fourth value of 0 (which is the w
):
v=[0100]
Combine into:
v′=[√0.50√0.50]][0100][√0.50−√0.50]]
To calculate the multiplication of two quaternions, you can use the Hamilton product. If you have the equation:
q3=q1∗q2
where each quaternion is composed in the following manner:
q1=q1w+q1xi+q1yy+q1zz
then using the Hamilton product, the values of each component in q3
are1:
q3w=q1wq2w−q1xq2x−q1yq2y−q1zq2zq3x=q1xq2w+q1wq2x−q1zq2y+q1yq2zq3y=q1yq2w+q1zq2x+q1wq2y−q1xq2zq3x=q1zq2w−q1yq2x+q1xq2y+q1wq2z
You can also rotate a vector →v
by a quaternion q
by decomposing the quaternion into it’s scalar part w
and it’s vector part →r
:
q=(w,→r)
and then using the following formula to calculate the rotated vector vrotated
:
vrotated=→v+2→r×(→r×→v+w→v)
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, RA
and then RB
, the total rotation RC
is found with:
RC=RBRA
Remember that quaternion multiplication is not associative, so the ordering of RA
and RB
is important.
Some Useful Quaternions
Quaternion | Description |
---|---|
q=[1,0,0,0] | Identity quaternion, no rotation. |
q=[0,1,0,0] | Rotation of 180 around X axis. |
q=[0,0,1,0] | Rotation of 180 around Y axis. |
q=[0,0,0,1] | Rotation of 180 around Z axis. |
q=[√0.5,√0.5,0,0] | Rotation of 90 around X axis. |
q=[√0.5,0,√0.5,0] | Rotation of 90 around Y axis. |
q=[√0.5,0,0,√0.5] | Rotation of 90 around Z axis. |
q=[√0.5,−√0.5,0,0] | Rotation of -90 around X axis. |
q=[√0.5,0,−√0.5,0] | Rotation of -90 around Y axis. |
q=[√0.5,0,0,−√0.5] | 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.
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:
axis=[axayaz]angle=θ
can be converted into a quaternion with:
q=[qwqxqyqz]=[cosθ2axsinθ2aysinθ2azsinθ2]
Use this tool to convert from a rotation expressed as an axis-angle to a quaternion:
θ | rad | |
x | ||
y | ||
z |
w | |
x | |
y | |
z |
Conversion From Quaternion To Rotation Matrix
If you have a quaternion in the form:
q=[qwqxqyqz]
Then the equivalent rotation matrix is:
R=[1−2q2y−2q2z2qxqy−2qzqw2qxqz+2qyqw2qxqy+2qzqw1−2q2x−2q2z2qyqz−2qxqw2qxqz−2qyqw2qyqz+2qxqw1−2q2x−2q2y]
Use this tool to convert quaternions to rotation matrices.
w | |
x | |
y | |
z |
3D Rotation
