Saturday 28 June 2014

A Gentle Introduction to Quaternions

The internet doesn’t have enough tutorials about quaternions so I thought I’d add another one. ;-)

You only need to learn the basic arithmetic and some formulae to enable you to get the hang of quaternions.

The multiplication sign, x in this article stands for ordinary, everyday multiplication.

Remember to to do the multiplications first in any sum before doing the additions.

Firstly I’ll introduce you to some unusual numbers that you don’t see in everyday life.

Lets call them alphabet numbers.



i-numbers

An i-number is a real number that is always multiplied by the letter i.

Examples of i-numbers are: 0 x i, 4 x i, –1 x i and 0.5 x i.

Usually the multiplication sign is left out when writing an i-number and you must remember that it is there, for example, 4 x i can be written as 4i.

You can add two i-numbers together which always results in another i-number.

For example, 4 x i + 2 x i = (4 + 2) x i = 6 x i

You can multiply an i-number by a real number which always results in another i-number or zero.

For example, 4 x i x 3 = (4 x 3) x i = 12 x i ; 1 x i = i ; 0 x i = 0.

You can multiply an i-number by another i-number but this results in a number and not an   i-number because of the first rule of the i-number club.

The first rule of i-number club is that i x i = –1.

For example, 4 x i x 2 x i = (4 x 2) x (i x i) = 8 x –1 = –8.

j-numbers

A j-number is a real number that is always multiplied by the letter j.

Examples of j-numbers are: 0 x j, 4 x j, –1 x j and 0.5 x j.

Usually the multiplication sign is left out when writing a j-number and you must remember that it is there, for example, 4 x j can be written as 4j.

You can add two j-numbers together which always results in another j-number.
For example, 4 x j + 2 x j = (4 + 2) x j = 6 x j

You can multiply a j-number by a real number which always results in another j-number or zero.
For example, 4 x j x 3 = (4 x 3) x j = 12 x j ; 1 x j = j ; 0 x j = 0.

You can multiply a j-number by another j-number but this results in a number and not a      j-number because of the first rule of the j-number club.

The first rule of j-number club is that j x j = –1.

For example, 4 x j x 2 x j = (4 x 2) x (j x j) = 8 x –1 = –8.

k-numbers

A k-number is a real number that is always multiplied by the letter k.

Examples of k-numbers are: 0 x k, 4 x k, –1 x k and 0.5 x k.

Usually the multiplication sign is left out when writing a k-number and you must remember that it is there, for example, 4 x k can be written as 4k.

You can add two k-numbers together which always results in another k-number.

For example, 4 x k + 2 x k = (4 + 2) x k = 6 x k

You can multiply a k-number by a real number which always results in another k-number or zero.
For example, 4 x k x 3 = (4 x 3) x k = 12 x k ; 1 x k = k ; 0 x k = 0.

You can multiply a k-number by another k-number but this results in a number and not a     k-number because of the first rule of the k-number club.

The first rule of k-number club is that k x k = –1.

For example, 4 x k x 2 x k = (4 x 2) x (k x k) = 8 x –1 = –8.


Adding alphabet numbers

Different alphabet numbers cannot be summed together into one total for the same reason you cannot sum apples and oranges together. You always have this many apples plus this many oranges not a total of this many apple_oranges.

Likewise when adding alphabet numbers you always get an i-number plus a j-number or an   i-number plus a j-number or a j-number plus a k-number or an i-number plus a j-number  plus a k-number.

Multiplying alphabet numbers

i-numbers, j-numbers and k-numbers can be multiplied by each other.

The first rule of multiplying different alphabet numbers is that the order of the letters in the multiplication must never be changed.

For example, 4 x i x 3 x j can be rewritten as 4 x 3 x i x j but never like this,  4 x 3 x j x i.

The second rule of multiplying different alphabet numbers is that i x j x k = –1.

This rule leads to the following results for the multiplication of two letters.
i x j = k
i x k = -j
j x i = -k
j x k = i
k x i = j
k x j = -i
Notice how i x j gives a different result than j x i. This is why you must never change the order of the letters in the multiplication of two different letters.

See this post for how to remember these rules [link].

Quaternions

A quaternion is the sum of four numbers - a real number plus an i-number plus a j-number plus a k-number.

For example, 3 + 4 x i + 2 x j + 5 x k.

Writing the quaternion with the real number first is one popular form but equally as popular is writing the real number at the end.

For example, 4 x i + 2 x j + 5 x k + 3.

In both cases the order of the alphabet numbers is i, j ,k.

Sometimes a quaternion is written as a group of four numbers without the letters and hopefully the writer tells you at which end the real number is otherwise you have to guess.

For example  4 x i + 2 x j + 5 x k + 3 is written as (4 2 5 3).

Any alphabet number can be considered a quaternion with the other three numbers equal to zero.
For example, the i-number 7 x i is the quaternion number 7 x i + 0 x j + 0 x k + 0.

Any real number can be considered a quaternion with the three alphabet numbers equal to zero.
For example the number 5 is the quaternion number 0 x i + 0 x j + 0 x k + 5.

Adding quaternions is straightforward – the real numbers are added together the i-numbers are added together, the j-numbers are added together and the k-numbers are added together.

For example, 4 x i + 2 x j + 5 x k + 3 plus 2 x i + (-4) x j + 1 x k + 7
equals 4 x i + 2 x i + 2 x j + (-4) x j + 5 x k + 1 x k + 3 + 7
equals (4 + 2) x i + (2 –4) x j + (5 + 1) x k + (3 + 7)
equals 6 x i + (-2) x j + 6 x k + 10
To multiply a quaternion by a real number you just multiply each of its four numbers by that real number.

For example, 4 x i + 2 x j + 5 x k + 3 times 2
equals (4 x i) x 2 + (2 x j) x 2 + (5 x k) x 2 + (3) x 2
equals (4 x 2) x i + (2 x 2) x j + (5 x 2) x k + (3 x 2)
equals 8 x i + 4 x j + 10 x k + 6
Multiplying a quaternion by another quaternion is time consuming but straightforward. Each number in the first quaternion is multiplied in turn by every number in the second quaternion and then the real numbers are added together, the i-numbers are added together,the j-numbers are added together and the k-numbers are added together.

The formula for multiplying two quaternions is as follows.

For quaternions, p = p1 x i + p2 x j + p3 x k + p4 and q = q1 x i + q2 x j + q3 x k + q4

p x q  = (p4 x q1 + p1 x q4 + p2 x q3 – p3 x q2) x i
+ (p4 x q2 + p2 x q4 – p1 x q3 + p3 x q1) x j
+ (p4 x q3 + p3 x q4 + p1 x q2 – p2 x q1) x k
+ p4 x q4 – p1 x q1 – p2 x q2 – p3 x q3
For example, Q = 4 x i + 2 x j + 5 x k + 3 times R = 2 x i  - 4 x j + 1 x k + 7
equals 56 x i + 8 x j + 18 x k + 16
If you calculate R x Q you will see that it is not the same as Q x R.
R x Q = 12 x i – 4 x j + 58 x k + 16
See this post for a technique to multiply quaternions by hand [link].

Using a Quaternion to Represent a Rotation

Not all quaternions are used to represent a rotation.

Only quaternions which satisfy the following equation are used for rotations.

Say quaternion Q = a x i + b x j + c x k + w .

If the square root of (a x a + b x b + c x c + w x w) equals 1 then the quaternion can be used for rotation.

These quaternions are known as unit quaternions.

The square root of (a x a + b x b + c x c + w x w) is known as the magnitude of the quaternion.

A quaternion can be transformed into a unit quaternion by dividing each of its four numbers by the magnitude.

Transforming a quaternion into a unit quaternion is called normalising the quaternion.

For every rotation there are two unit quaternions.

If you have one unit quaternion representing a rotation, the other unit quaternion is obtained by multiplying each number of the first unit quaternion by the number –1.

Rotation is an amount of turning around a line that points in some direction in 3D space.

Here we will only consider rotations about a line that passes through the origin (0, 0, 0) of the coordinate system.

This line is more commonly known as an axis and its direction can be specified by three numbers, usually the X, Y, Z coordinates of a point on the line other than the origin.

The amount of turning is commonly specified by an angle in degrees.

If you know the angle and axis of a rotation you can construct a quaternion from them to represent the same rotation.

Say X, Y, Z are the coordinates of the axis and theta is the angle.

If X x X + Y x Y + Z x Z = 1 then the quaternion for the rotation is:
sin(theta/2) x X x i + sin(theta/2) x Y x j + sin(theta/2) x Z x k + cos(theta/2)

If X x X + Y x Y + Z x Z is not equal to 1 the quaternion for the rotation is:
sin(theta/2) x X/L x i + sin(theta/2) x Y/L x j + sin(theta/2) x Z/L x k + cos(theta/2)
where L is the square root of (X x X + Y x Y + Z x Z).

If you calculate the magnitude of the quaternions created by the two formulae above you will see that the formulae create unit quaternions (allow for rounding errors).

Lets create some quaternions but first we have to decide on a coordinate system.

Consider the coordinate system with the positive X direction pointing to the right and the positive Y direction pointing up the screen.

We now have two choices for the direction the positive Z points – into the screen or out of the screen.

If we choose into the screen we have what is called a left handed coordinate system.

If we choose out of the screen we have what is called a right handed coordinate system.


We will choose to have the positive Z pointing out of the screen and always specify coordinates in the order (x, y, z).

Coordinate System

A counter-clockwise rotation when looking down the axis of rotation towards the origin is considered positive. This is the so called right hand screw rule for rotations.


clockwise (red) and counter clockwise (blue)

The following graphs of sine and cosine will be useful.



Example 1) 0 degrees about the Y axis.

The Y axis can be specified by the coordinates (0, 1, 0)
0 x 0 + 1 x 1 + 0 x 0 = 1 so we can plug the coordinates straight into the formula.
Q = sin(0/2) x 0 x i + sin(0/2) x 1 x j + sin(0/2) x 0 x k + cos(0/2)
Q = sin(0) x 0 x i + sin(0) x 1 x j + sin(0) x 0 x k + cos(0)
Q = 0 x 0 x i + 0 x 0 x j + 0 x 0 x k + 1
Q = 0 x i + 0 x j + 0 x k + 1 or in shorthand notation (0 0 0 1)


Example 2) 45 degrees about the X axis

The X axis can be specified by the coordinates (1, 0, 0)
1 x 1 + 0 x 0 + 0 x 0 = 1 so we can plug the coordinates straight into the formula.
Q = sin(45/2) x 1 x i + sin(45/2) x 0 x j + sin(45/2) x 0 x k + cos(45/2)
Q = sin(22.5) x 1 x i + sin(22.5) x 0 x j + sin(22.5) x 0 x k + cos(22.5)
Q =  0.3827 x 1 x i + 0.3827 x 0 x j + 0.3827 x 0 x k + 0.9239
Q = 0.3827 x i + 0 x j + 0 x k + 0.9239 or in shorthand notation (0.3827 0 0 0.9239)

Example 3) 90 degrees about the Z axis

The Z axis can be specified by the coordinates (0, 0, 1)
0 x 0 + 0 x 0 + 1 x 1 = 1 so we can plug the coordinates straight into the formula.
Q = sin(90/2) x 0 x i + sin(90/2) x 0 x j + sin(90/2) x 1 x k + cos(90/2)
Q = sin(45) x 0 x i + sin(45) x 0 x j + sin(45) x 1 x k + cos(45)
Q =  0.7071 x 0 x i + 0.7071 x 0 x j + 0.7071 x 1 x k + 0.7071
Q = 0 x i + 0 x j + 0.7071 x k + 0.7071 or in shorthand notation (0 0 0.7071 0.7071)

Example 4) 180 degrees about the Y axis.

The Y axis can be specified by the coordinates (0, 1, 0)
0 x 0 + 1 x 1 + 0 x 0 = 1 so we can plug the coordinates straight into the formula.
Q = sin(180/2) x 0 x i + sin(180/2) x 1 x j + sin(180/2) x 0 x k + cos(180/2)
Q = sin(90) x 0 x i + sin(90) x 1 x j + sin(90) x 0 x k + cos(90)
Q = 1 x 0 x i + 1 x 1 x j + 1 x 0 x k + 0
Q = 0 x i + 1 x j + 0 x k + 0 or in shorthand notation (0 1 0 0)

Example 4) 360 degrees about the Y axis.

The Y axis can be specified by the coordinates (0, 1, 0)
0 x 0 + 1 x 1 + 0 x 0 = 1 so we can plug the coordinates straight into the formula.
Q = sin(360/2) x 0 x i + sin(360/2) x 1 x j + sin(360/2) x 0 x k + cos(360/2)
Q = sin(180) x 0 x i + sin(180) x 1 x j + sin(180) x 0 x k + cos(180)
Q = 0 x 0 x i + 0 x 1 x j + 0 x 0 x k + (-1)
Q = 0 x i + 0 x j + 0 x k - 1 or in shorthand notation (0 0 0 -1)

Example 5) 70 degrees about an axis through the point (1, 1, 1).

The axis is specified by the coordinates (1, 1, 1)
1 x 1 + 1 x 1 + 1 x 1 = 3 so we cannot plug the coordinates straight into the formula.
The distance to this point is the square root of 3 = 1.7321.
Therefore the normalised axis is (1/1.7321, 1/1.7321, 1/7321) = (0.5774, 0.5774, 0.5774)
Q = sin(70/2) x 0.5774 x i + sin(70/2) x 0.5774 x j + sin(70/2) x 0.5774 x k + cos(70/2)
Q = sin(35) x 0.5774 x i + sin(35) x 0.5774 x j + sin(35) x 0.5774 x k + cos(35)
Q = 0.5736 x 0.5774 x i + 0.5736 x 0.5774 x j + 0.5736 x 0.5774 x k + 0.8192
Q = 0.3312 x i + 0.3312 x j + 0.3312 x k + 0.8192 or (0.3312 0.3312 0.3312 0.8192)

From these examples we can make a table for quick reference.


RotationQuaternions (Real number first)Quaternions (Real number last)
no rotation(1 0 0 0)(0 0 0 1)
45 degrees(0.9239 ? ? ?)(? ? ? 0.9239)
90 degrees(0.7071 ? ? ?)(? ? ? 0.7071)
180 degrees(0 ? ? ?)(? ? ? 0)
360 degrees(-1 ? ? ?)(? ? ? -1)


From the table we can see that if the real number of a unit quaternion is close to 1it is a small rotation but if the real number is close to 0 it is a rotation near 180 degrees.

We also see that the quaternion for no rotation times –1 equals the quaternion for a 360 degree rotation which should be the case since these two rotations are equivalent.

Representing a Sequence of Rotations with a Quaternion

A sequence of rotations can be combined into a single quaternion.

Say the first rotation applied is represented by quaternion, Qfirst
The next rotation applied is represented by quaternion, Qsecond
The last rotation applied is represented by quaternion, Qlast

You generate the quaternion to represent the total rotation by multiplying the quaternions for each rotation in the sequence together.

The first rule of combining the rotations is that the order of the quaternions in the multiplication is, from left to right, last rotation to first rotation.
Qtotal =     Qlast x Qsecond x Qfirst

Rotating a Point with a Quaternion

When a point is rotated about an axis its position changes to new (X, Y, Z) coordinates.

To calculate the new position using quaternions you have to use two quaternions.

If you have a unit quaternion representing a rotation you generate a second quaternion from it by changing the sign of the alphabet numbers to the opposite sign.

This second quaternion is known as the inverse of the first quaternion.

For example, if Q = 0.3312 x i + 0.3312 x j + 0.3312 x k + 0.8192.

The inverse quaternion Q-1 =  -0.3312 x i - 0.3312 x j - 0.3312 x k + 0.8192.

To calculate the new position for a point p = (X, Y, Z) rotated by a rotation Q you first create a quaternion for the point as follows. Note it does not represent a rotation so do not normalise it.
P = X x i + Y x j + Z x k + 0
Then the quaternion P is multiplied on the left by Q and multiplied on the right by Q-1.
result = Q x P x Q-1
This multiplication is known as a sandwich product since the point’s quaternion is “sandwiched” between two Q quaternions.

The result of the multiplication is a new quaternion where the:
  • the real number of the i-number is the X coordinate of the point’s new position
  • the real number of the j-number is the Y coordinate of the point’s new position
  • the real number of the k-number is the Z coordinate of the point’s new position
For example, consider the point (2, 0, 0) rotated 90 degrees about the Y axis.

The rotation quaternion is Q = 0 x i + 0.7071 x j + 0 x k + 0.7071
The inverse quaternion is Q-1 = 0 x i – 0.7071 x j + 0 x k + 0.7071
The point quaternion is P = 2 x i + 0 x j + 0 x k + 0

The sandwich product Q x P x Q-1 is:

(0 x i + 0.7071 x j + 0 x k + 0.7071) x (2 x i + 0 x j + 0 x k + 0) x (0 x i – 0.7071 x j + 0 x k + 0.7071)

Calculating the Q x P part using our formula, the sandwich product becomes
(1.4142 x i + 0 x j – 1.4142 x k +0) x (0 x i – 0.7071 x j + 0 x k + 0.7071)
Using the formula again we get the resultant quaternion.
0 x i + 0 x j – 2 x k + 0
The point at (2, 0, 0) is now at position (0, 0, -2) which is what we expect from a rotation of 90 degrees about the Y axis.

Point rotated 90 degrees

Note we could have calculated P x Q-1 first in the sandwich product and still got the same result.

 Things you should know

The alphabet numbers are known in mathematics as Imaginary Numbers.

The real number of a quaternion is known as the Real Part.

The three imaginary numbers of a quaternion are known as the Imaginary Part.

The Imaginary Part is also known as the Vector of a quaternion.

I am not a mathematician so there may be errors.

Links

I suggest you do not go near the Wikipedia quaternion article unless you are a mathematician.
  1. http://tutis.ca/Rotate/
  2. http://3dgep.com/?p=1815
  3. http://www.euclideanspace.com/
  4. http://www.youtube.com/user/BSVino
  5. http://www.tobynorris.com/work/prog/csharp/quatview/ quaternion viewer program
  6. http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors
  7. http://www.ogre3d.org/tikiwiki/Quaternion+and+Rotation+Primer
  8. http://antongerdelan.net/opengl/quaternions.html
  9. http://www.sacredsoftware.net/tutorials/Quaternions/Quaternions.xhtml
  10. https://www.reddit.com/r/gamedev/comments/1bkjbq/need_some_help_understanding_rotation_quaternions/ 
  11. https://www.reddit.com/r/gamedev/comments/15ijcx/what_is_the_math_behind_finding_an_objects_vector/ 

No comments:

Post a Comment