Please explain Quaternions!

Hi everybody. I rather new to Unity and don’t understand quaternions. How are the four components related and how do you calculate a quaternion from euler angles.
I am not asking for a function, but for a explanation.


Firstly I should point out that you will probably never need to know any of this to successfully code in Unity and, unless you become very knowledgeable about Quaternions, should probably never set them directly. That is not to say you can’t, but it is generally advised against unless you know what you are doing!

Secondly, there is a heck of a lot of information out there on the internet on the subject of quaternions and/or converting them to Euler angles (and other rotation representations).

Thirdly, for anyone who is looking for just the function to handle all this for you checkout:

  1. Quaternion.eulerAngles
  2. Quaternion.Euler

The Basics

A quaternion rotation is made up of 4 numbers, whose values all have a minimum of -1 and a maximum of 1, i.e (0, 0, 0, 1) is a quaternion rotation that is equivalent to ‘no rotation’ or a rotation of 0 around all axis.

Quaternions are widely used in coding as:

  1. They are almost always unique, as opposed to Euler angles where 360 degrees around an axis is the same as 0 around the same axis, is the same as 720… etc, this confusion does not exist with quaternions, where every orientation is related to 2 quaternion, q and -q.
  2. They are simple to create/compose
  3. They are ‘easy’ to interpolate between (as they are not commutative i.e ab =/= ba), whereas Euler Angle representation suffers from ‘gimbal lock’.


All rotation quaternions are unit quaternions (their length is 1).


Angle-Axis to Quaternion

Given a normalized (length 1) axis representation (x, y, z) and an angle A.

The corresponding quaternion is equal to:

Q = [sin(A/2)*x, sin(A/2)*y, sin(A/2)*z, cos(A/2)] (corresponding to [x, y, z, w])

Euler angle to Quaternion
Given an Euler rotation (X, Y, Z) [using orthogonal axes]

x = sin(Y)sin(Z)cos(X)+cos(Y)cos(Z)sin(X)
y = sin(Y)cos(Z)cos(X)+cos(Y)sin(Z)sin(X)
z = cos(Y)sin(Z)cos(X)-sin(Y)cos(Z)sin(X)
w = cos(Y)cos(Z)cos(X)-sin(Y)sin(Z)sin(X)
Q = [x, y, z, w]

Slerp (Spherical linear interpolation)

To interpolate between two quaternions is very simple:

Let t be a value that increases from 0 to 1.

The angle (A) between the two quaternions can be found using the dot product:

Q1Q2 = Q1.xQ2.x+Q1.yQ2.y+Q1.zQ2.z+Q1.w*Q2.w = A

then the un-normalized interpolation (Q3) is (sin((1-u)A)/sin(A))*Q1 + (sin((uA)/sin(A))*Q2

to find the normalized quaternion you need to divide by Q3’s length.

Q3/|Q3| = Q4

Hence, Q4 is a Quaternion rotation between Q1 and Q2 based on t.

Links/Extra reading

  1. Euler to Quaternion
  2. Quaternion To Euler
  3. AngleAxis to Quaternion
  4. Quaternion to AngleAxis

On these pages heading is the rotation around y, attidue around z, and bank around the x axis.


I am nowhere close to an expert at quaternions, so I apologise if anything I have written is incorrect. I marking this as a community wiki, so feel free to change something if its incorrect!


Well actually it seems, I must be genius or utterly retarded, I don’t know yet, but as whydoidoit said, it would be a nice approach to have, still the quaternions seem to be more complicated than i would like to accept. yo then i finally have to pose a question^^

Could you all please stop saying this all “You don’t need to understand this” bullshit. Of course you need to. quaternions are part of the basics in all of unity and actually I don’t understand why xyz rotation is used when you could simply use quaternions. Because they aren’t much different but have an additional angle value.

So here’s the deal: quaternions are vector3s with an additional rotation angle. The direction the vector points actually IS the rotation. So think of it as a rotation not based on its personal xyz dimension but on the unified global xyz dimension. This additional rotation angle actually is the personal ‘roll’ ( like rolling the vector like a cigarette. Smoking sucks btw) and is the last component of having maximal rotation freedom.

The reason why you think it’s complicated is because mathematicians tend to be unable to explain shit. So they keep the obvious mystical, especially when they keep the benefit of knowledge from you to make themselves a necessary evil. Or because of their organizational blindness. But I stick with the first reason ,because the second is just an excuse. :wink:

You don’t need to understand Quaternions directly, just how you should use them. Been using Unity for years and you just need to understand they are a representation of an angle, not how they work. it’s wasted effort most likely.
Why do you want to understand them and then maybe we can work out what you really want to know :slight_smile: If you’re just worried that they are mysterious, don’t worry, I was like that too but unless you’re deep into a 3D math problem I’d ignore them for now.

Everything is simple when you know how, so don’t let Quaternions be any different.

One of the main reasons people don’t understand Quaternions is because they can’t visualise them, or the effect they have on a rotation. There are many suggested reasons as to why Quaternions are hard to visualise, but my theory is that natural mathematicians aren’t great visual explainers - it’s not that they’re holding back information, simply don’t know how to explain it in a way us more visual learners can comprehend.

So, the best way to ‘visualise’ a quaternion is this:

Imagine you have a small lump of clay and a load of tooth pics.
Slide a tooth pick through the clay on each of it’s individual axis, X, Y and Z.
Rotate the tooth picks, notice how the clay is being rotated on one of it’s axis at a time. This is how normal Euler axis work, and how most people understand rotation.
Now, grab another tooth pick and jam it into the clay at any angle you want!
Rotate the new tooth pick on it’s own axis, notice it isn’t constrained to the x, y or z axis but is rotating on it’s own ‘new’ and unique axis.
This ^ is a quaternion, whenever you are reading up on them from now on, think of this example and hopefully it should make a little more sense.

I hope this isn’t too much of a necro, but I came here while on the search for a better understanding of quaternions and later found several resources which earlier me would have benefited from at the point I was reading this page.

3Blue1Brown is a fantastic youtube channel explaining so many areas of mathematics in a brilliant, visual style. Grant Sanderson and Ben Eater put together an absolutely excellent tutorial series with interactive applet on this. Honestly, if you’ve any questions about quaternions and particularly if you’re into game design (so you’re probably a very visual learner), go check it out:

This gave me a much better understanding of what quaternions are, and why you multiply them in Unity (and why order matters).
For reference (and because the unity documentation is coy about this), the w component is the real component and x, y and z are the imaginary i, j, and k factors in unity’s implementation.

Look at this short video explaining Quaternions advantages and drawbacks.

,Look this shot video explaining Quaternions advantages and drawbacks.