2D Rotation
A 2D rotation has only one parameter, . When the basis vectors and are rotated by an angle :
Which builds the rotation matrix:
When a vector is transformed by this matrix, we know that the vector will be a linear combination of the basis, which are and :
Using a matrix to encode this operation:
See also
complex numbers
.
3D Rotation
About Cardinal Axes
See also:
About an Arbitrary Axis
Given an axis and an amount of rotation around it , our goal is to find a rotation matrix that rotates about by the angle :
The basic idea is to solve this problem in a plane perpendicular to , which becomes a 2D problem.
Separate into two vectors: a vector parallel to called and a vector perpendicular to called such that :
After the rotation, it’s obvious that the component will be the same, and only the vector will be rotated.
A plane can be defined with two vectors that lie on it. Since we have and we also know the normal of the plane (which is ), any vector perpendicular to both vectors will also lie in the plane. We can use the cross product to find this vector:
The length of is:
Which means that has the same length as . Note that even though they have the same length, they don’t necessarily have unit length.
and now form a 2D coordinate space where the -axis is and the -axis is .
Let be a vector that is the result of rotating by an angle . We can find the projection of it onto the -axis and the -axis as follows:
- Expressing as a linear combination of the basis:
Reconstructing the solution from the observations above:
Finally:
Now we can compute what the basis vectors are after the transformation above (by using each of the basis vectors as on ) to construct a rotation matrix:
Constructing the matrix from these vectors:
3D Rotations Using Quaternions
A complex rotor is a unit norm complex number that rotates another complex number by the angle and has the form:
Hamilton had hoped that a unit-norm quaternion could be used to rotate a vector, which is stored as a pure quaternion . The unit norm quaternion is given by:
Let’s compute the product :
Special Case
What if is perpendicular to ? Then the scalar quantity of is zero, and we are left with the pure quaternion:
Let’s analyze the vector part of (which is now a 3D entity because it’s a pure quaternion). Since is perpendicular to , then the vector will have a norm equal to . Also, since is a unit vector, then , which means that we have two orthogonal vectors with the same length.
To rotate the vector about , let’s transform to the 2D space whose basis vectors are and and perform the rotation there, which is trivially . Therefore, all we have to do in is make the scalar quantities multiplying each vector equal to the projection of the rotated vector over the basis:
Which makes the quaternion have the form:
And it acts as a rotor only when is perpendicular to .
Important notes/facts about orthogonal quaternions:
- If is a rotor about the unit vector by an angle whose vector term is perpendicular to the pure quaternion :
- and rotate by an angle about .
- and rotate by an angle about .
- Each of these products leaves unscaled (because is a unit norm quaternion).
General Case
Let’s use as the starting point. Note that this time its vector part is not necessarily perpendicular to the pure quaternion . The product yields:
Note that the term does not vanish since for the general case, and are no longer perpendicular. What’s more important is that the product is no longer a pure quaternion. Multiplying a vector by a non-orthogonal quaternion has converted some of the vector information into the quaternion’s scalar component.
What happens if we post-multiply by ? Could it reverse the operation? (Note that since is a norm quaternion, .)
Let’s first check if doing this multiplication makes the scalar component vanish:
Indeed, it magically made the scalar component vanish! Now let’s look at the vector component of :
Let’s
expand the cross product
:
Therefore:
Let’s make and , just like in (it worked as a rotor when it was orthogonal to ; it might work with the general case too):
Which involves double-angle terms. Replacing these terms with
double-angle identities
:
The product created a pure quaternion equal to rotated by an angle of . If we want to rotate by an angle of , we must build a half-angle quaternion (note above that was equal to ):
Using , the product is:
Note that the vector part of is identical to .
Quaternion Difference and Dot Product
Let and be two unit norm quaternions (rotors that have the same form as ). The quaternion to rotate from to is given by and is known as quaternion difference. Finding the value of given that we know and :
Expanding the product:
Note that the scalar part of this quaternion is equal to the inner product (a generalization of the dot product to abstract vector spaces) between two quaternions:
Remembering that a rotor is given by , we can relate the inner product between rotor quaternions with the scalar quantity of and interpret it geometrically, just like the dot product between two vectors in 3D/2D space, but this time noticing that the dot product gives the cosine of half the angle between the quaternions:
This means that the angle between and is equal to:
Or using the
half-angle formulas
:
The second formula works for all cases, as noted
here
(the first one doesn’t work when ).