 Home /
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.
Answer by Scribe · Feb 21, 2014 at 04:16 PM
Hello!
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:

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:
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.
They are simple to create/compose
They are 'easy' to interpolate between (as they are not commutative i.e a*b =/= b*a), whereas Euler Angle representation suffers from 'gimbal lock'.
Note:
All rotation quaternions are unit quaternions (their length is 1).
Conversions
AngleAxis 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:
Q1*Q2 = Q1.x*Q2.x+Q1.y*Q2.y+Q1.z*Q2.z+Q1.w*Q2.w = A
then the unnormalized interpolation (Q3) is (sin((1u)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
On these pages heading is the rotation around y, attidue around z, and bank around the x axis.
Disclaimer
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!
Scribe
Answer by deCalle · Mar 27, 2016 at 08:18 PM
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. ;)
Answer by guy123 · Sep 15, 2016 at 01:18 PM
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.
Answer by tdev · Feb 21, 2014 at 03:35 PM
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 :) 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.
Your answer
Welcome to Unity Answers
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Follow this Question
Related Questions
Really Confused about Scripting Rotations 2 Answers
Calculating angles to a target and turning towards it 1 Answer
Manual Quaternions 3 Answers
Child versus Parent rotations 3 Answers
3D nested Turret Prefab Rotation 1 Answer