In today’s post, I explain the use of quaternions for rotations.
Background
What’s a quaternion?
A quaternion is a 4-element vector, typically represented as . ( can go on either the start or the end of the vector.)
In our very limited context, a quaternion can be used to represent a vector, and a rotation about that vector.

With this system, we can represent any rotation.
Why not Euler angles?
Euler angles (roll, pitch, and yaw) have three issues I care about:
- Gimbal lock - when applying rotations, if two axes become aligned, they’re the same axis now and a degree of rotation freedom is lost permanently :(
- Interpolation troubles - because each axis interpolates by itself, the most direct path isn’t always taken. This matters a LOT for control systems.
- Poorly documented conventions - Since it matters in what order you apply each Euler angle (roll, pitch, yaw), it’s necessary to define and stick to a convention. There is no universal convention, which opens up the possibility of misinterpretation.
Quaternions solve all of these troubles, while being mathematically beautiful.
How do I make one?
A detailed analysis of quaternion math is both beyond the scope of this article and my knowledge. Therefore, I will stick to practicals.
To represent a rotation of about a unit vector , use the half-angle theorem:
which can also be represented as:
which will be important very shortly.
How do I rotate a vector using a quaternion?
You need a base vector, , and a rotation to apply, . The rotated quaternion, , is defined as follows:
( is the conjugate of , which just means that the imaginary components of have their signs flipped. This conjugate multiplication also ensures we end up with a rotated Cartesian vector, and not another quaternion!)
This begs a VERY important question:
How do I multiply two quaternions together?!
It’s easy to see how to add quaternions together (you do it piecewise, like ordinary vectors), but surely piecewise multiplication will leave a horrifying mess of s, s, et cetera. This is where Sir William Rowan Hamilton innovated when he invented quaternions, and quaternion multiplication is aptly done via the “Hamilton product”:
Multiply the quaternions piecewise, and then simplify using the following brilliant identities:
The result is your new rotated quaternion!
Rotation Example
Say that we wanted to rotate a vector by 90 degrees around the axis (as in this Stack Exchange post):
What we’re solving for is , so we need to find and and then conduct a couple Hamilton products.
Our desired rotation quaternion is defined with half angles:
We’ll need the conjugate later, which is just with its imaginary parts negated:
The first Hamilton product we’ll carry out is , as follows:
which, using the identities above, simplifies to:
Now we can solve for by multiplying by :
Simplifying once again using the quaternion identities:
This matches up perfectly with what we expect: the axis rotated by 90 degrees around the axis is equal to the negative axis.
Thanks for reading!