 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.
You need to understand the maths behind Quaternions, have you checked this article on the matter? http://3dgep.com/?p=1815
Should convert to answer. Explain Quaternions, Ill do that if you explain women.
Is it just me or does unitygems not work?
I'm just getting "page ok" from that link.
I don't think so. IT it very much important to know how it works.If someone want to implement something advance it is required. Isthere anyone who will explain using graphical presentation.
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
Would like permission to add this to the Unity Gems quaternions tutorial as a reference if you don't mind.
awesome! Thank you for the explanation. Quaternions are one of those dark corners of Unity coding that I've used and been very curious about, but mostly have just left alone in a box marked "Black Magic  Do Not Open". While I still am going to leave it in that box, this has helped shine a small light on it for a moment, so I can be a little less afraid ^_^
@whydoidoit  Of course! I haven't seen your tutorial but if you think I've missed anything that you've explained there feel free to add it, or put it as a link under 'Links' :)
@zombience glad I could help! I feel like I should be quoting J. R. R Tolkien  "May it be a light to you in dark places, when all other lights go out."
P.S how do I @ you zombience :'(
The Quaternions tutorial is about how to use them  almost explicitly the opposite of what you explained here  which I did not feel qualified to write :)
Appended to article, here for your reference: http://unitygems.com/quaternionsrotationspart1c/#appendix
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.
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. ;)
Quaternions are counter intuitive. They are also complicated for beginners and nonmathematicians. (Despite being an engineer and understanding them) Ive never once needed to actually know the workings of a Quaternion to use them in Unity. The methods provided actually take out a lot of the need to know how they work. In that sense they become some intermediate block that just gets converted from and to and not worried about beyond that.
Of course, everyone should eventually learn it, but it is certainly true that you dont need to to use them in Unity and its a bit of a waste of time, to be honest (with so much other stuff that you need to learn to simply operate Unity and there not being enough time in the day).
I think the need for quaternions is sometimes not well understood. I see it like this. A set of Euler angles can easily represent a line pointing in any possible direction. However we are rarely dealing with lines, our objects have volume. As soon as an object has volume then that shape can also be rotated around the direction it's facing. Hence the need for 4 values to describe the exact rotation of something which isn't a line.
Now it so happens that in many orientations you could represent all four of these axes of rotation using 3 world axes  however it's clear that there must be places where we run out of dimensionality using just 3 values to represent 4 things. Hence the need for quaternions.
I see @deCalle's point  as soon as you realise that a Euler angle misses a dimension of rotation that clearly always exists, then you start to wonder why the hell you'd use any other representation which is clearly flawed.
So a quaternion is a representation of a vector and a rotation around that vector. BUT the individual elements are interdependent, you can't just go around modifying them individually without screwing everything up. Quaternions are considered a "thing" that can be used as a value in a calculation, but unlike vectors or Euler angles you can't just go and change one component to get a simple expected effect.
Basically if a quaternion was just a normalized direction and a number which was the rotation around that direction then it would all be super simple. But they aren't that. Each element of a quaternion is multiplied by the sin or cos of the angle of rotation.
Avoiding gimbal lock is important and you can trivially use "whole" quaternions to point anything at anything else. Changing the individual values  err, well, can't say I ever do :)
You know the more I think about @deCalle's point the more I think it would be really nice if we had a rotation component that was X,Y,Z,W where each was the rotation in degrees around world X,Y,Z axes and the direction entailed. Would be really nice to use that :) Internally it would have to form a quaternion, but could provide such an interface  would make life much clearer
This explanation is wrong. Print a vector, then print the quaternion you get from LookRotation  they are different.
You can think of a Quaternion as an y,x,z rotation, or as a vector3 plus a spin, or as a rotation matrix, or as a single rotation around some axis. And that's the point  think of rotations however you like, them let the system handle them through Quaternions.
Quats aren't a Unity thing  they're standard in game engines. The reason is they handle combining or (s)lerping or anglebetween well. Other methods of representing orientation aren't as good at performing those operations.
Well a quaternion is a vector and a spin. Just the mathematical representation of that is the vector and a variety of sines and cosines of the angle of spin.
Double complex rotation it is:
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 Roobubba · Aug 05, 2019 at 09:34 AM
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:
https://eater.net/quaternions/
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.
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
Calculating angles to a target and turning towards it 1 Answer
Really Confused about Scripting Rotations 2 Answers
Manual Quaternions 3 Answers
Child versus Parent rotations 3 Answers
3D nested Turret Prefab Rotation 1 Answer