Quaternions: A Powerful Way to Rotate

Reading Time: 5 minutes

What is a quaternion? What do quaternions represent? And how do you calculate a quaternion? What are the components of a quaternion? These are not the kind of questions that likely keep you up at night. And if you came here to get answers to some of those questions? I’m sorry, but you’ll leave woefully uninformed. But approach me and ask:

What are quaternions good for?

And I will have some answers for you. Firstly, I might be overly pedantic and point out that you ended your sentence with a dangling participle, because I tend to use humor to diffuse awkward social interactions and you just approached me from out of nowhere to ask me a very specific question. Sarcastic grammar nitpicks aside, my answer to your question is simply that quaternions are a superior and, in my opinion, intuitive way to represent rotations in three-dimensional space.

Before we get into the meat and potatoes of this post, I want to get a couple of things out of the way. First and foremost, this post is heavily inspired by an excellent video I watched on YouTube some time ago. I’ve been fighting back and forth about whether or not to make this post simply for the reason that I’m not entirely sure I’m adding anything of substance to the source material. It feels clearly derivative and yet not derivative enough to stand on its own merit. Despite my reservations, I’ve been bullied into posting and I really think more people should know about quaternions so what the heck. But in my best effort to head off any accusations of plagiarism, which I’m sure nobody will actually level and is only a fear that lives in my overly anxious head, please go watch their original video. It’s excellent and probably more effective at visually presenting this information.

Secondly, if you genuinely want to learn more about quaternions, what they are, other ways to use them, or have deeper questions, Ben Eater and Grant Sanderson (of 3blue1brown fame) has an excellent and thorough interactive post explaining them in more depth. Go there, Grant is far more intelligent and more qualified than I.

Thirdly, and probably most importantly, I’m not a math expert. I’ve done my best to research the basics of what I’m covering, but I’m as fallible as any other human and even moreso here, given this isn’t my area of expertise. Still here? Well, then let’s get into it! First let’s cover what you likely use for rotation systems currently.

The Euler Coordinate System

The most common system taught, and likely the one you were primarily exposed to, consists of 3 angles of rotation about a fixed coordinate system. These 3 angles, sometimes referred to as Euler angles, are very simple to teach given the framework most math students have by the time they’re ready to learn geometry. They’re simple to visualize and demonstrate, and they are practical. Aircraft and spacecraft often use Euler angles because they are directly measurable by gimbals (a series of rings that suspend a gyroscope that spins to maintain its orientation while the vehicle rotates as needed) for reference. Mentioning the gimbal brings up the obligatory mention of the big flashy reason mechanical engineers hate Euler angles: gimbal lock.

Gimbal Lock

All you need to know about gimbal lock is that it’s bad, but it’s avoidable. All it describes is a situation wherein you align two axes of rotation along the same plane which causes those two axes to provide the same rotation, eliminating a degree of freedom from the system.

Gimbal Lock

While gimbal lock is the big flashy reason why Euler angles are less than ideal, I don’t find them as nearly problematic as I was lead to believe I’d find them. Truly quicksand levels of misplaced concern. Like I understand how, if you happen to encounter it while piloting a vehicle travelling at insane speeds, yeah of course that’s a serious problem. But to the everyday human, on the extreme off chance you encounter gimbal lock, you can simply back out of your rotations and attempt another sequence of Euler angles. Simply put, while it is possible to rotate your system such that you lose an entire degree of freedom, every single orientation is obtainable by some sequence of Euler angles.

Euler Isn’t Commutative

I know, I know. That’s a big word for Elmo. Hold on. Remember those silly seeming properties they had you learn in algebra? Well turns out they are useful to know. Now I’m not out here defending algebra curriculums everywhere, I hated algebraic proofs as much as anyone else. But what’s important is that the final orientation of your object depends on the order in which you apply your rotations. This is arguably the most annoying property of Euler angles (in my opinion).

Now, I want to clarify, this isn’t some crippling weakness. Generally speaking, this isn’t an issue as long as you maintain consistency. Having to constantly think about things like what rotation order the program you’re using defaults to is just tedious to me. Just for example, here are the defaults of some pretty common programs:

Godot – YXZ

Unity – ZXY

Unreal – XYZ (world space); ZYX (local space)

Blender – XYZ

As you can see there’s no industry standard and I don’t even want to discuss the difference between world space and local space (and why on Earth Unreal uses different defaults for each).

And because Euler angles aren’t commutative, the order matters. Below is a demo of -90º rotations on all 3 axes in each rotation order:

So what is the solution?

Quaternions!

Okay, math majors please look away. I’m about to provide not only a gross oversimplification of quaternions, but I’m also going to be skimming over what they actually are and mostly focusing on how they’re used as a 3D orientation system.

Quaternions, VERY Simply

What are quaternions, really? I’m not fully sure and it’s not really relevant, but it’s basically an extension of the complex numbers. They’re generally presented in the form:

a + bi + cj + dk

Quaternions, For Rotation

Hold on, don’t let me lose you, what’s really important is that you let the computer do all the math for you and I’ll get to the “intuitive” part. Imagine that the center of your object is the origin of a unit vector (a vector with length 1). Now point that vector down the axis you want to rotate around. Now rotate around your new custom axis as desired. Boom. Quaternions are superior. What does this look like in practice? I’ll show you:


# First you need to make sure you normalize the vector
ijk_vector = Vector3(i_value, j_value, k_value).normalized()
quaternion_rotation = Quaternion(ijk_vector, deg_to_rad(w_degrees))

That’s it. And guess what? You can interpolate them. You can animate them. They aren’t order dependent. You can combine them easily. You can invert them. They are way cheaper to calculate. No gimbal lock. They’re better in every way. AND you can implement them today. Below I’ll provide a fun tool to help you visualize it better, but there’s nothing stopping you from implementing quaternions in your project today.


Leave a Reply

Your email address will not be published. Required fields are marked *