Research archive · original page Research.Quaternions · last recorded update 2013-10-10
Summary
- Equivalent in power to Gibbs vectors, with dot and cross products.
- Easier than vectors for 3D rotations, e.g.,
- combining multiple rotations
- animating a rotation
- other possibilities for rotations:
- matrices
- Euler_Angles - very bad for animation, and suffer from Gimbal_lock
- Rotation3D
- Invented by WR Hamilton in 1843 as a 3D analog to complex numbers, to do 3D rotations. However, what we use today is a distilled essence of the complicated way that Hamilton presented them. (That is true for many mathematical ideas.)
Advantages
- Quaternions start from the intuitive axis-angle API.
- Animating a large rotation in small steps (by varying the angle slowly) is easy. In contrast, stepping the 3 Euler angles does not work well, and there's no obvious way to gradually apply a \(3\times3\) rotation matrix, \(M\). (You could compute \(M^{1/100}\) and apply it 100 times, but that computation is messy.)
- When combining multiple rotations, the axis and angle of the combo is easy to find.
- Having only 4 parameters to represent the 3 degrees of freedom of a 3D rotation is the right number. Using only 3 parameters, as Euler angles do, causes gimbal lock. That is, you cannot always represent a smooth rotation by smooth changes of the 3 parameters. OTOH, using 9 parameters, as with a matrix, gives too much opportunity for roundoff errors causing the matrix not to be exactly a rotation. (You can snap the matrix back to a rotation matrix, but that's messy.)
Refs
- my paper
- Guha, p 253-268.
- http://en.wikipedia.org/wiki/Quaternion
- http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
- http://en.wikipedia.org/wiki/History_of_quaternions
- The Matrix and Quaternions FAQ
- Rotations in Three Dimensions, Part Five: Quaternions
- many other places
The math
- Review complex numbers: \(z=x+iy\). \(x\) and \(y\) are real. The only things we know about \(i\) are these:
- We can multiply it by a real, and add it to a real, \(2+3i\).
- We can add multiples, e.g.: \(2i+3i=5i\).
- When multiplying \(i\) by \(i\), we can replace \(i^2\) by \(-1\). (It is wrong to call \(i\) the square root of -1. Doing that leads to paradoxes because -1 has 2 square roots. There is no way to tell which one is the positive square root; that concept has no meaning for an imaginary, and trying to do that causes paradoxes.)
- Quaternions have 3 things like \(i\) (they're called indeterminates): \(i\), \(j\), and \(k\).
- You can add them and form expressions like \(1+2i+3j+4k\).
- When multiplying, you combine them thus:
- \(i^2=j^2=k^2= -1\)
- \(ij=-ji=k, jk=-kj=i, ki=-ik=j\) Note that they are not commutative. That is required since 3D rotations are not commutative.
- To rotate using quaternions, take a rotation with axis \((a_x,a_y, a_z)\) and angle \(\theta\). The axis must be normalized: \(a_x^2+a_y^2+a_z^2=1\).
- Form the quaternion \ \[q= \cos\left(\frac{\theta}{2}\right) + \sin\left(\frac{\theta}{2}\right) \left(a_x i + a_y j + a_z k \right) \]
- Form its conjugate \ \[q^*= \cos\left(\frac{\theta}{2}\right) - \sin\left(\frac{\theta}{2}\right) \left(a_x i + a_y j + a_z k \right) \]
- The point you want to rotate is \((p_x,p_y,p_z)\); form the quaternion\ \[p = p_x i + p_y j + p_z k \]
- Then the rotated \(p\) is \ \[ p' = q p q^* \]
- To rotate by \(q_1\) and then \(q_2\), compute\ \[ p' = q_2 q_1 p q_1^* q_2^* \]
- Optimize it by computing the combined rotation\ \[ q = q_2 q_1 \]
- and then applying it:\ \[p' = q p q^* \]
Sample question
What rotation does this quaternion represent:
\(q = 0.707 + 0.707 j\)Answer: a \(90^\circ\) rotation about the axis \((0,1,0)\)
Examples
Start with some 2D geometry and complex number examples.
Let the point \(p=(1,2)\). The corresponding complex number is \(c=1+2i\). Suppose you want to rotate it by \(\theta=90^\circ=\pi/2\) radians. That is equivalent to multiplying \(c\) by \(e^{i\theta}=e^{i\pi/2}=i\).
So, \(c'=c e^{i\theta} = (1+2i)i = -2+i\).
The corresponding 2D point is \((-2,1)\).
Now to quaternions in general.
Let \(q_1=(1,2,0,0)\) and \(q_2=(3,0,4,0)\). Then \(q_1+q_2=(4,2,4,0)\).
Now to 3D and quaternions.
For example, the 3D point \((1,0,2)\) corresponds to the quaternion \(1i+0j+2k\).