Introduction:
This tutorial explains how quaternions work and how they can be used to produce 3D rotations. Basics of linear algebra and trigonometry
are required for this tutorial. Complex numbers and polar coordinates are also important, but the important parts are summarized in the next
background section. This tutorial works its way up from 2D rotations and how its related to complex numbers, and then it extends that to the 3D
space to show how quaternions produce 3D rotations.
Quaternions are very common in computer graphics and animation because of some useful properties, such as avoiding
gimbal lock, which is a major problem with euler angles.
Background:
Polar coordinates:
Polar coordinates are coordinates expressed in terms of angle and magnitude .
A cartesian coordinate
is equivalent to the polar coordinate
where:
Polar coordiantes can be converted back into cartesian coordinates using the properties of and :
Complex numbers:
Complex numbers have the form , where is the real part,
is the imaginary part, and is the imaginary unit which is the square root of .
In other words, .
Complex numbers can be represented in both cartesian and polar coordinates. In cartesian coordinates, the axis is the real axis, and the axis is the imaginary axis:
Similarly, a complex number can be expressed as polar coordinates by substituting:
This gives us:
Two complex numbers
and can be multiplied as follows:
In polar coordinates, this multiplication is equivalent to:
Then simplying with trigonometric identities, we get:
In other words, multiplying two complex numbers creates a new complex number where the original angles have added up, and the magnitudes have multiplied.
Complex numbers and 2D rotation:
Let's say you have a cartesian point
and you want to rotate it by radians.
As shown in the background section, this point
is equivalent to the polar coordinate such that:
When this point is rotated by radians, we get a new point
which is equivalent to the polar coordinate :
Expanding this out using the trigonometric identities, we get:
Note that as shown in the background section of complex number multiplication, this is equivalent to multiplying the complex numbers
and :
Simplying using trigonometric identities, this can be simplified to:
This means complex numbers with magnitude 1 naturally encode 2D rotation; multiplying a point with a complex number
rotates the point by . For example, let's say you want to rotate a point by radians, this is equivalent to the complex number:
This shows us that multiplying by is equivalent to rotating by radians. See this picture which shows multiplication by on
the key points , , , and :
|
Representing 2D rotations as matrices:
As shown in the previous section, the point
can be rotated by as follows:
This can be expressed as the matrix vector multiplication:
Another way to derive this matrix is represent and as matrices:
This means the rotation complex number
can be expressed as:
Euler's formula:
Euler's formula states that
.
This can be shown as follows:
The Maclaurin expansion of is:
The Maclaurin expansion of is:
The Maclaurin expansion of is:
This means that putting to the power of gives:
Using Euler's formula, a rotation of the vector
by , can be expressed as follows:
3D Rotation:
A vector can be rotated in 3D space around any given unit vector representing the axis of rotation using Rodrigues' rotation formula:
Note that is rotated radians around .
To derive this formula, we start by breaking the vector into
and such that:
- is the part of parallel to
- is the part of perpendicular to
The parallel vector is the projection of onto :
Note that since is a unit vector, so we have:
After is found, we can calulate as follows:
When the rotation is applied, will remain the same, and
will rotate on the plane with the normal .
This means the rotated vector will be a sum of
and :
Note that is rotated
radians on the plane with normal .
The vector is
rotated radians on the plane with the normal . The rotated perpendicular vector
will be a sum of this vector and :
These vectors can be used to calculate the rotated perpendicular vector using sine and cosine:
All of these definitions can be combined to form the rotation formula:
This formula can be converted into a matrix multiplication by factoring out the original vector :
To use Euler angles, this matrix can be turned into seperate matrices for each axis , , and :
Quaternions:
Quaternions are a 4D number system of the form .
Quaternions extend the complex number system with 2 additional dimensions and .
This chart (from Wikipedia) shows the multiplication rules for the basis vectors , , and (the left column is the first term and the top column is the second term):
Note: Row Column
Note that since the multiplication of the basis vectors is not commutative (i.e. ), quaternion multiplication is not commutative.
The multiplication result of two quaternions and can be derived using this chart:
Quaternions can also be expressed as a pair with a real part ,
and a scalar part :
In this form, the multiplication can be expressed in terms of dot products and cross products:
Note that this scalar/vector product is the exact same calculation as the manual multiplication of quaternions derived above:
Lastly, the conjugate of a quaternion , denoted by , is simply its vector part multipled by :
The product of a quaternion and its conjugate is:
This means that if is a unit quaternion, this product will be equal to 1. This fact will be useful later on.
Quaternion rotation demo:
Visualizing how quaternion multiplication represents rotations is extremely helpful for understanding how quaternions can be used to represent 3D rotation.
It is a bit hard to see, but this demo has 3 cubes in it: a center cube, an inner cube, and the outer cube. The center cube represents the real dimension's point .
The outer cube's parts all represent the real dimension's point .
The colored spheres represent the initial points:
,
,
,
,
,
,
,
.
The inner cube represents the 3D imaginary space of the , , and axis. The colored spheres can identify
which dimension an inner part represents (for example: the inner cube part with the red sphere is the dimension).
Lastly, the colored arrows represent a quaternion multiplication. The red arrows represent a multiplication by ,
the green arrows represent a multiplication by , and the blue arrows represent a multiplication by .
Notice which location each initial point takes after a multiplication. For example, a muliplication by causes the initial points to move as follows:
Note that this creates two rotations. The first rotation happens on the axis and follows the regular 2D complex number rules/rotation, and
affects the and real dimensions. The second rotation happens on the and axis/dimensions.
This demo shows that quaterion multiplication does a 3D rotation as expected, but also does a 2D rotation which we want to avoid.
In the section "Quaternions and 3D rotation", we will discuss this more and show how the additional 2D rotation can be cancelled out to produce just the 3D rotation.
Euler's formula for quaternions:
Euler's formula can be extended to quaternions by expanding the Maclaurin series of ,
where is a pure vector quaternion (no scalar part):
Evaluating the first few quaternion terms, we see the pattern:
This means equals:
If is a unit vector, we get:
This gives us:
This can be simplified further using the following facts:
This leaves us with:
Quaternions and 3D rotation:
Applying the quaternion to a vector produces the result:
This product is not a pure 3D rotation - it introduces the scalar component ,
which makes the product a 4D point. Another problem is that the vector parts rotation happens on the plane formed by the vectors and
- which is the plane with normal
.
Also note that when the angle between and is not a multiple of ,
the lengths of and will not be the same and so the
vector part will also end up being scaled between and .
This multiplication is equivalent to applying to both the
and parts:
This shows that the multiplication
has side effects on . For a valid 3D rotation, should not be affected like this.
Also note that the the multiplication
produces
, which is from Rodrigues' rotation formula.
If these side effects can be removed from , the product will be exactly
the rotated vector from Rodrigues' rotation formula.
Note that we cannot apply to only because this equation only applies the rotation to the vector part of
which is perpendicular to , and that operation cannot be converted to a single matrix vector multiplication. To build matrices which can be applied to any arbitrary 3D vector , the rotation must be applied to both
and :
The side effects on can be cancelled out by multiplying with the quaternion conjugate:
Note that this can also be accomplished by right multiplying with the conjugate:
Also note what happens when is right multiplied by the conjugate:
This means the operation
does not change , but it does rotate to compute , which gives the final result:
In other words, remains the same, and
gets rotated by
around . This is exactly Rodrigues' rotation formula, but with the angle of rotation doubled. This can be worked around by simply dividing the angle of rotation by two,
.
Converting quaternions to matrices:
The operation
can be converted into a matrix multiplication by factoring out :
The scalar part of the product becomes:
The vector part of the product becomes:
This has just become Rodrigues' rotation formula. As shown previously, this can be converted directly into the matrix-vector multiplication:
This rotation matrix can be generated directly from a given quaternion
with the following substitutions using trigonometric identities:
Then simplifying a bit more with factoring and the fact that :
Euler angle's to quaternions:
A quaternion can be created from Euler angles as follows.
First construct a quaternion for each axis:
Then combine them all to form the combined quaternion:
Quaternion to axis angle:
Let
be the quaternion used to represent a rotation of radians around the axis .
The angle of rotation can be read from the scalar component :
The axis of rotation can be obtained by normalizing the vector component :
Rotation matrix to quaternion:
Let be the rotation matrix generated from the quaternion :
The quaternion can be read from the matrix as follows:
This only works if is greater than zero. These are the other equivalent calculations if is zero:
Sources:
- Euclidean Space - Conversion Matrix to Quaternion: https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
- Khan Academy - Maclaurin series of cos(x): https://www.khanacademy.org/math/ap-calculus-bc/bc-series-new/bc-10-14/v/cosine-taylor-series-at-0-maclaurin
- Khan Academy - Maclaurin series of sin(x): https://www.khanacademy.org/math/ap-calculus-bc/bc-series-new/bc-10-14/v/sine-taylor-series-at-0-maclaurin
- Khan Academy - Maclaurin series of e^x: https://www.khanacademy.org/math/ap-calculus-bc/bc-series-new/bc-10-14/v/taylor-series-at-0-maclaurin-for-e-to-the-x
- Khan Academy - Euler's formula & Euler's identity: https://www.khanacademy.org/math/ap-calculus-bc/bc-series-new/bc-10-14/v/euler-s-formula-and-euler-s-identity
- Wikipedia - Complex numbers: https://en.wikipedia.org/wiki/Complex_number
- Wikipedia - Polar coordinates: https://en.wikipedia.org/wiki/Polar_coordinate_system
- Wikipedia - Quaternions: https://en.wikipedia.org/wiki/Quaternion
- Wikipedia - Quaternions and spatial rotation: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
- Wikipedia - Rodrigues' rotation formula: https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
- Youtube - 3Blue1Browns's video "Quaternions and 3d rotation, explained interactively": https://youtu.be/zjMuIxRvygQ
- Youtube - 3Blue1Browns's video "Visualizing quaternions (4d numbers) with stereographic projection": https://youtu.be/d4EgbgTm0Bg
- Youtube - Mathoma's video "3D Rotations in General: Rodrigues Rotation Formula and Quaternion Exponentials": https://youtu.be/q-ESzg03mQc
- Youtube - Mathoma's video "3D Rotations and Quaternion Exponentials: Special Case": https://youtu.be/UaK2q22mMEg
- Youtube - Mathoma's video "Euler's Formula for the Quaternions": https://youtu.be/88BA8aO3qXA
- Youtube - Mathoma's video "Quaternions as 4x4 Matrices - Connections to Linear Algebra": https://youtu.be/3Ki14CsP_9k
- Youtube - Mathoma's video "Quaternions EXPLAINED Briefly": https://youtu.be/jlskQDR8-bY