If you’ve ever dived into the world of 3D graphics, game development, or robotics, you’ve probably heard of quaternions. They sound intimidating, right? Like some kind of dark magic reserved for math wizards. But don’t worry, in this post I’ll break it all down for you in a way that’s easy to understand, even if you’re not a math expert. By the end of this post, you’ll have a solid grasp of what quaternions are, why they’re useful, and how they work.
Table of Contents
What Are Quaternions?
Quaternions are a number system that extends the complex numbers. If you’ve ever worked with complex numbers (you know, those numbers with an imaginary part like \(3+4i\), what are imaginary numbers and his meaning it’s a matter of another post), quaternions are like their bigger, more powerful cousins. But instead of just one imaginary part, quaternions have three imaginary parts. A quaternion is typically written as:
\( q=w+xi+yj+zk \)
Here:
- \( w \) is the real part.
- \(x\),\(y\),\(z\) are the imaginary parts.
- \(i\) ,\(j\) ,\(k\) are the imaginary units, and they have some special properties (we’ll get to that in a bit).
Quaternions are super useful for representing rotations in 3D space. If you’ve ever tried to rotate an object in 3D using Euler angles (roll, pitch, yaw), you might have run into a problem called gimbal lock. Quaternions solve this problem elegantly, and they’re also more efficient for computers to work with.
if you don’t know what gimbal lock is, this video will help you to understand the problem
Why Should You Care About Quaternions?
Before we dive into the math, let’s talk about why quaternions matter. Imagine you’re building a 3D game, and you need to rotate a character’s arm. You could use Euler angles, but as the arm rotates, you might lose a degree of freedom (that’s gimbal lock). Quaternions avoid this issue entirely. They also interpolate smoothly, which means you can create smooth animations without weird jumps or flips.
in short terms quaternions are compact; they use only four numbers to represent a rotation, eficient; they’re faster to compute than other methods and stable; they avoid gimbal lock and other rotation issues.
The Math Behind Quaternions
Now, let’s get into the nitty-gritty. Don’t worry, I’ll keep it simple and friendly!
You may be asking yourself, if I’m not a math guy/girl, why do i need to know the math behind quaternions? I only need to know how to use it right? -Well, you may be surprised but knowing; or at least knowing a little about the mathematics behind quaternions, will give you an idea of how to use them effectively in your developments. So let’s get our hands dirty!
The Imaginary Units i,j,k
The imaginary units \(i,j,k\) are the heart of quaternions. They have some special rules for multiplication:
\(i^2 + j^2 + k^2 = ijk = -1\)
This means:
- Multiplying \(i\) by itself (\(i^2\)) gives \(−1\).
- The same goes for \(j\) and \(k\).
- Multiplying all three together (\(ijk\)) also gives \(−1\).
These rules might seem weird at first, but they’re what make quaternions work, the mathematical demostration of this properties are beyond the scope of this post, if you want to deep dive in this theme I recomend you the book Math for Game Developers.
This imaginary numbers properties also lead to some interesting properties when multiplying quaternions together.
Quaternion Multiplication
Multiplying two quaternions isn’t as straightforward as multiplying regular numbers, the result is similar to the cross product of vectors, in that the result yields to another quaternion and it’s not commutative . Let’s say we have two quaternions:
The formula for quaternion multiplication can be easily derived based upon the definition of quaternions as complex numbers, the result of \( q_1q_2 \) is:
Yeah, that looks complicated and hard to learn (not actually), i know it because i love to know this things but don’t worry, you don’t need to memorize this. The key takeaway is that quaternion multiplication is not commutative, meaning \( q_1q_2 \) is not the same as \(q_2 q_1\). This is a big deal and one of the reasons quaternions are so powerful.
The quaternion multiplication is also known as the Hamilton product
Representing Rotations with Quaternions
Here’s where quaternions really shine. To represent a rotation in 3D space, we use a unit quaternion, which is a quaternion with a magnitude of 1. A unit quaternion looks like this:
\( q_1 = (1,0,0,0) \)
We can convert back the unit quaternion to axis-angle representation as follows:
\( q_1 = \cos{\frac{\theta}{2}} + \sin{\frac{\theta}{2}(x_1i + y_1j + z_1k)} \)
- \( \theta \) is the angle of rotation.
- \( (x_1i + y_1j + z_1k) \) are the axis of rotation.
So, if you want to rotate an object 90 degrees around the x-axis, you’d use:
\( q_1 = \cos{\frac{\pi}{4}} + \sin{\frac{\pi}{4}} \)
\( \pi/4 \) radias are \( 90^{\circ} \) degrees.
Combining Rotations
One of the coolest things about quaternions is how easy it is to combine rotations. If you have two rotations represented by quaternions \(q_1\) and \(q_2\), you can combine them by multiplying the quaternions: \(q_{total}=q_2q_1\).
Notice the order: \(q_2q_1\) means “apply \(q_1\) first, then \(q_2\).” This is because quaternion multiplication is not commutative, as I mentioned earlier.
Why Quaternions Are Better Than Euler Angles?
Euler angles (although are also useful to represent rotations and mathematically very interesting I’ll not cover them in this post -maybe another day) are another way to represent rotations, but they have some serious drawbacks:
- Gimbal Lock: When two axes align, you lose a degree of freedom.
- Interpolation Issues: Smoothly interpolating between two Euler angles can lead to weird twists and flips.
Quaternions solve both of these problems. They’re also more computationally efficient, which is why they’re used in everything from video games to spacecraft navigation.
Why we use quaternions in game development?
In game development, especially in 3D games, you’re constantly dealing with rotations. Whether it’s turning a character, rotating a camera, or animating objects, rotations are everywhere. Unity, the game engine I use and one of the most popular game engines, uses quaternions under the hood to handle all of this.
Unity uses quaternions to represent rotations internally. If you’ve ever worked with Unity’s Transform
component, you’ve probably seen the rotation
property. This property is a quaternion! However, Unity also provides a more user-friendly way to work with rotations using Euler angles (roll, pitch, yaw). Behind the scenes, Unity converts these Euler angles into quaternions.
In Unity, you can create a quaternion using the Quaternion
class. Here are some common ways to create quaternions:
From Euler Angles
If you’re more comfortable working with Euler angles, you can convert them to a quaternion using Quaternion.Euler
:
// Rotate 45 degrees around the X-axis
Quaternion rotation = Quaternion.Euler(45f, 0f, 0f);
This creates a quaternion that represents a 45-degree rotation around the X-axis.
From Axis-Angle
You can also create a quaternion using an axis and an angle. This is useful when you want to rotate around a specific axis:
// Rotate 90 degrees around the Y-axis
Vector3 axis = Vector3.up; // Y-axis
float angle = 90f;
Quaternion rotation = Quaternion.AngleAxis(angle, axis);
This creates a quaternion that represents a 90-degree rotation around the Y-axis.
Applying Quaternions to GameObjects
Once you have a quaternion, you can apply it to a GameObject’s rotation. Here’s how:
// Rotate a GameObject
public Transform myObject;
void Start() {
Quaternion rotation = Quaternion.Euler(45f, 0f, 0f);
myObject.rotation = rotation;
}
This code rotates myObject
by 45 degrees around the X-axis.
Combining Rotations
One of the most powerful features of quaternions is their ability to combine rotations. In Unity, you can multiply quaternions to combine them:
Quaternion rotation1 = Quaternion.Euler(45f, 0f, 0f); // Rotate around X
Quaternion rotation2 = Quaternion.Euler(0f, 90f, 0f); // Rotate around Y
Quaternion combinedRotation = rotation1 * rotation2; // Combine rotations
myObject.rotation = combinedRotation;
This code first rotates the object around the X-axis and then around the Y-axis. The order matters! Remember, quaternion multiplication is not commutative.
Looking at a Target
A common use case in games is making an object look at a target. Unity provides a handy method for this called Quaternion.LookRotation
:
public Transform myObject;
public Transform target;
void Update() {
Vector3 direction = target.position - myObject.position;
Quaternion targetRotation = Quaternion.LookRotation(direction);
myObject.rotation = Quaternion.Slerp(myObject.rotation, targetRotation, Time.deltaTime);
}
This code makes myObject
smoothly rotate to face the target
on each frame.
Rotating Objects Over Time
In games, you often need to rotate objects over time. Here’s how you can do that using quaternions:
public Transform myObject;
public float rotationSpeed = 50f;
void Update() {
float angle = rotationSpeed * Time.deltaTime;
Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.up);
myObject.rotation *= rotation;
}
This code rotates myObject
around the Y-axis at a speed of rotationSpeed
degrees per second.
Conclusion about Quaternions
Quaternions are a fundamental tool in game development, and Unity makes it easy to work with them. Whether you’re rotating objects, animating characters, or making a camera follow a target, quaternions are your best friend. They might seem intimidating at first, but once you get the hang of them, they’re incredibly powerful and versatile.
I don’t run ads in my blog, if this content its useful to you in the future, please considere to invite me to drink a coffe.