Why Quaternions?
Hello, I'm wondering why does Unity use Quaternions?
Since I'm almost every time use Euler angles (and I'm sure I'm not alone), why do we need Quaternions?
Thanks.
Google it. Quaternions are essentially too complicated to understand without a decent grasp of maths, certainly too complicated for someone to explain easily on here. I mean I can't make it more than a few lines into the Wikipedia page on them without my eyes glazing over and none of the 'dumbed down' explanations I've read have enlighten me any. Granted I did spend large amounts of time in maths at school just shading in graph paper bored out of my $$anonymous$$d but I don't remember ever hearing the word 'Quaternion' before I used Unity.
They just work, that's all you need to know.
No, they don't work. When making a Quaternion from Euler angles, even when I specify that rotation around Z should be 0, Quaternion is initialized and my character (slightly, st. around +/- 0.9) rotates around the Z axis.
Right well this sounds more like an issue with floating point accuracy rather than quaternions themselves and you'd have probably been given a solution by now if you posted the problem rather than just asking 'Why Quaternions?'
Without seeing the code that rotates your object it is hard to help but setting localEulerAngles.z to 0 after your rotation rather than during probably will fix it.
For example: in my project I have an object which I want to track another object so it is using Quaternion.LookRotation and Quaternion.RotateTowards. Problem was that even if the objects were at exactly the same height the Y value would still end up being modified by small values. That's due to floats being inherently inaccurate.
The solution in this case was to put this after the Quaternion rotations:
exampleObj.localEulerAngles = new Vector3(0,exampleObj.localEulerAngles.y, 0);
That causes the object to only rotate on the y axis and keeps x and z at exactly 0.
Answer by meat5000 · Apr 04, 2016 at 01:29 PM
Quaternions were invented to overcome the Euler problem of Gimbal Lock.
Hm, I don't see huge disadvantage, but I accept this as one possible answer.
The classic real world scenario of Gimbal Lock being a problem is in the rotational movement of spacecraft. The Gimbals become aligned and can never free themselves.
A Gimbal is a hoop mounted on an axis, basically. The other axes are mounted within the last Gimbal. It was made for Gyroscopy. This is basically the same way that the Axes behave in Unity so the same phenomenon occurs.
Theres a video that does a decent job of explaining it.
Answer by IgorAherne · Apr 03, 2016 at 05:16 PM
Quaternions are just 4-component vectors, with xyzw.
They are more expensive to create than matrices, but are much cheaper to update once created. They are only 4-float structure, whereas matrices are 16-float. This reduces the size of data during runtime.
This is another part of the answer. I know that they're more efficient, but if you just store 3 floats, and then apply that rotation around gimbal (even that you can get "locked"), it will be, in my opinion, efficient too.