The Infamous Quaternion

Hello people,

What is a Quaternion? Unity doc goes about as far as saying ‘it is a magical thing that you shouldn’t try to understand that makes rotations work’…‘quaternions don’t cause gimbal lock!’

Youtube has some very pretty videos demonstrating Gimbal Lock.

Wikipedia says something about quaternions in hieroglyphs; I haven’t found anyone able to understand the Wikipedia page on Quaternions.

Nobody on the IRC channel seems to understand them beyond the formalities of how to talk to them.

Searching Google hasn’t given me anything either.

Can someone please explain with some mathematical rigour (I am not afraid of the maths, please bring it on!) what is going on here?

I remember one of my school friends showing me years back how you can accomplish any 3D transform + translation by using a 4x4 matrix. To understand this you can lose a dimension and examine how you can achieve any { 2D transform + translate } by using a 3x3 matrix; you can see visually how on the XY plane everything is mapping to where you want it, and elsewhere in 3-space it is going a bit crazy, it is some ugly shear or something. But it all that we are concerned about is the behaviour on the XY plane.

And this has advantages, because matrix composition, inversion, nesting of transforms, … it is worth doing this apparently.

But a quaternion is something else, is it not? It is some sort of extension of complex numbers…

Is there any way to intuitively understand this topic?

Well, I can’t answer any further than what you already know, but here’s a couple of links I had bookmarked on this topic:

Maths - Quaternions
The Matrix and Quaternions FAQ

Good luck!

Quaternions aren’t infamous. They are curious. I think this page also explains how and why WR Hamilton invented them.

I had a CS professor explain Quaternions in 2-dimensional translations. What crazy linked describes it similarly. When we move an object, we transform it in some way, these transforms are all handled by matrices. The problem is, using the conventionally stored matrix data, we can only do one thing at a time, which is both inefficient and frustrating. By adding the extra dimension to the matrix, we multiply as the article linked states. But, it also lets us compose our movements, enabling us to do translations and rotations and everything else fun all at once, without breaking everything down.

Yes it certainly helped me to understand in 2D first as well. Interestingly I’ve read some very angry opinions expressed about Quaternions vs Matrices (article by Diana Gruber).

lol… I always giggle when I watch people try to chase down what a quaternion is… and there’s a reason.

Quaternions are high math, they’re complicated shit, you don’t learn about these until you’re years into your math studies. And there’s a reason, there’s a lot of foundation work that you MUST know to understand them… and even then they’re a trip to deal with.

You’ll see people with CS degrees and engineers pop around (edit: of course as I way typing this a couple popped in and did this very thing) and talk about how they’re really just necessary parts of math and attempt to explain them expecting the listener to have also taken the advanced math classes. We who have taken math seldom remember when we were young kids and sums and products were daunting things.

But math is most certainly one of those you can’t take a step until you’ve mastered the previous. You’ll never understand complex numbers, and 4 dimensional products until you’ve learned everything under it.

So I’ll go threw what foundations you need, what parts of said foundations, and why you need them.

  1. Algebra - you need to understand the abstract representational concepts taught here. The idea of what a variable is, the logic, the… it’s the foundation of all foundations… it’s also where you’ll first be introduced to numeric structures like Matrices and Vectors (a special kind of matrix). If you haven’t aced algebra… sorry, there is NOTHING I can do for you.

You may wonder why I said algebra and started so low… shit I could have started at arithmetic. But I start here because I know a lot of people entering the game-design field often only have a basic high school Algebra education, and even then only passed with a C.

  1. Trig Geometry - I usually like to lump trig and geometry together… geometry is the proofs and theories of angles and shapes, trig is the math, trig is the actual numbers of everything geometry. If you don’t understand your trig functions… you’ll be sccrewed. If I say “tangent curve” and you can’t see the tangent curves in your head… or if I say “sine of pi over 2” and you don’t immediately spit back “1”… well I can’t help you, you’re screwed. Go learn your math! Don’t forget your arc-trig functions, and your hyper-trig functions, and all your relationships. If you don’t know sin^2 + cos^2 = 1… well what the heck? Pythagoros!

You need the understanding of angular relationships to get why quaternions work… they have to do with rotations in imaginary space! Angles are like important!

  1. Calculus - you actually probably don’t NEED anything from calculus. It’s not like quaternions have a lot to do with integration or anything. But Calc is still a basic tier stuff, and you’ll need it to do any of the higher stuff.

  2. Complex/Imaginary Numbers - now we’re getting into the meat of it. The imaginary number is the square-root of -1, or ‘i’. Complex numbers are any numeric expansion of i, using i as the identity value.

a * i = imaginary a

thusly expanded all imaginary numbers can be described as:

c = ia + b

where a and b are any real numbers. Think about this from Algebra, when you learned the slope-intercept form of a line…

f(x) = mx + b

notice how they look similar? You essentially have a curve (lines are curves) with an imaginary slope… this means it gradients into non-space.

  1. Linear Algebra - now here’s where we get to start putting this stuff together. In linear algebra you learn about spacial transformations. This is where all that matrix arithmetic you learned in algebra pops up its head. And you learn how a matrix can be used to tranform multi-dimensional information.

You have a non-functional curve? Rotate it around the origin with a rotation matrix until its functional (line test), perform you math, then rotate back.

You have a 4 dimensional object that needs to be projected into 3-space for a real-world approximation? Use a projection matrix.

You have a 3 dimensional image that needs to be projected onto a 2-D monitor (like a video game)? Use a projection matrix.

This is linear algebra!

Matrices aren’t defined as representing any given thing. They’re just a numeric structure. You get to pick what it describes and shape it to your needs. Compare a colormatrix to a camera-projectionmatrix to a transform-matrix in video-games, all matrices, all do the same math, non alike in what they are designed to accomplish.

Now remember how I said vectors are a special kind of matrix. It’s just a matrix of a specific shape, used for specific things. And we use them in unity for things like translation (which is a transformation).

Well Quaternions are a special kind of vector, which means they’re also a special kind of matrix! The math is all the same. Cross products and scalar products (dot products) are all inherited from matrix math that you learned back in algebra.

Quaternions just have a more specific use, and rotating objects in 3-space isn’t the only use… it’s just the use WE use them for… just like vectors aren’t only for translation.

The design of a quat is that of complex numbers. It’s a transformation described in imaginary space. How this transformation appears in unity is as a rotation (a quat can actually do more, like scale and invert, but we always normalize our quaternions to avoid this as it’s look weird on screen).

Now… if you don’t know all that stuff up to there… well, you aren’t going to wrap your head around quaternions. As they’re still a bit of a ways off from here.

[EDIT]

At CrazySi - actually they are rather infamous amongst novices in the game-design world who never took Computer Science like you implied you did. If you had never seen a quat, and came into game design, saw that bad boy sitting there… what do you think your reaction would be?

Ha, well said lordofduct!

Quaternions aren’t something to learn and understand, they are a tool.
You only have to learn how to apply them, and recognize when to use them.
Learn how they are different from simple rotations.

[Edit] And learn what they represent. A 3D vector plus a roll rotation.
Ha, I think that’s what they are! Dang quaternions!
People, correct me if I’m wrong. I too am a quaternion noob.

Yeah in the end… everything I said has to do with understanding them.

But you don’t need to fully understand them to use them though.

Most people don’t know how a car works, and yet they’re all over my roads… ::my inner truck-driver shakes fist at the 4-wheelers::

LordOfDuct – spam spam spam eggs and spam. Enough spouting of high school maths. Please! Do you actually have a conceptual grasp on quaternions? If so, please share! If not, please get off the pot.

SlyDog – ‘Quaternions aren’t something to learn and understand, they are a tool.’
:eyes:
This question specifically asks for understanding.

Slev – what you are describing is exactly what I noted in the question: using a fourth dimension to handle translations.
I’m familiar with the mathematics of doing this. However, it appears that this is something completely separate from the quaternion.

Crazy-Si: that is a good article link. Thanks! I’m going to repost it together with the follow-up article, as it is a 2-parter:

I found a good article here: http://www.isner.com/tutorials/quatSpells/quaternion_spells_14.htm

Wasn’t spam, it was the mere fact that I have no idea where you stand in the realm of mathematical knowledge. You could be a 14 year old in high school, you could be a CS major, you could be a kid in art school, for all I know.

I have 0 background as to what you know, and you complained about the complexity of the quaternion… so I pointed out if you don’t even know mathematical basics, you ain’t got a chance brotha. And not knowing mathematical basics is actual fairly common… even in the game community.

SPAM is more like “here buy my stuff”.

Obviously I failed at my attempt at pointing out, in an over the top comedic way while still informative, that asking a complicated question with little background to your question makes for serving complicated answers difficult. But sorry, that doesn’t make me the one with no concept of quaternions… it makes you the one with no concept of quaternions because you were the one who asked in the first place.

If all you wanted were articles… a damn google could have done that for you. But you wanted our explanation, and where would I start in my own explanation if I don’t even know if you understand what imaginary numbers are.

So you wasted my time, so I wasted yours. Now go:

So it sounds like you know what a Gimbal Lock is, but here it is spelled out…

Take an object. Turn it 90 degrees on it’s Z axis. What just happened? The Local Y axis becomes the World X axis and the Local X axis turns into the World Y axis (assuming Y is up).

A quaternion is basically the result of a mathematical function performed on your euler degrees, such that you end up with a normalized Vector(x,y,z) and a ‘w’ to rotate along that normalized vector.

Think of Direction and Speed… velocity = Normalized Direction Vector * Speed

It is kind of the same concept, with a few differences, and calculating it all by hand isn’t something most of us know how to do, which is why we just use the build in functions to do it all automagically for us.

q = w + xi + yj + zk

Seeing some actual code or formulas of how they are converted might help you understand what represents what, and why most of us could really care less how they work, as long as they DO work.

Haven’t seen much on the benefits of quaternions in the replies. I see a lot of repeating themes mentioning Gimbal Lock though.
Not as a test to show off, but to expand the discussion (with OP’s blessing? :slight_smile: ).
So pub quiz, no cheating by googling, see if you can have at least one answer for:

  1. Why even use quaternions?
  2. Why are quaternions are so popular, especially in Game Engines?
  3. What are the alternatives? Are the other methods more simple?
  4. What benefits do the alternative methods have over quaternions?

I think if you can give reasonable answers to those, then at least you can do street kungfu with them.

Quaternions are more compact allowing for smaller storage space and speedier calculations and they avoid computational anomolies (gimbal locks).

Alternatives include the two most common matrices and euler angles. Euler angles are the most compact but are computationally heavy. Matrices are the most bloated (16 matrix positions, though 4 can be implied making 12, still more bloated), the computations are far more speedier than euler angles, but quats are still faster.

The benefits of matrices are that they also store more information all in one, including translation and axis independent scales. Euler angles benefits include being very easily human-readable.

(my answers are relative to game engines, the answer for their mathematical creation was covered in a previously supplied link, which was to facilitate the multiplication of triples)

I have a couple of questions related to quaternions and their usage in Unity:

  • Why does Unity change the values I insert to the “Local rotation”? :face_with_spiral_eyes: If I for example now put 0.55 into the camera, it changes it A LOT into 0.5238. What’s up with this? And why does it also change the other two of the “X”, “Y” and “Z” values?
  • Is it so that earlier Unity versions used Euler angles? At least some tutorials seem to show euler angles in the “local rotation”.
  • Is it possible to use euler angles for inserting the local rotation even if under the hood Unity would use quaternions?
  • Is there an online quaternion to euler angle converter? I found the simple way to do that through code, but an online converter would be nice too. All I could find was the opposite:
    Online Conversion - Quaternion Conversion

You bumped a old thread for questions. Really bad practice.