# Where can I learn about quaternions and how to manage rotations in Unity

I would like to learn how to use quaternions with sample projects and/or visual explanations for better understanding. Any urls, books or videos on rotations ???

I did not get any answers on that question( align to floor in local X - Questions & Answers - Unity Discussions ), and I would like to solve that myself and understand 100% of the code I use anyway…

A quaternion is a math representation of a single rotation around an axis - not only the familiar x, y and z axes, but an axis oriented in any direction. Any 3 axes rotation can be converted to a single rotation of some angle around a well calculated axis, what avoids gimbal lock and eases interpolation, thus the Unity team decided to use this approach.

There are two functions, Quaternion.ToAngleAxis and Quaternion.AngleAxis, that convert quaternions to and from the angle/axis representation. Quaternion.ToAngleAxis(out angle, out axis) returns a float in angle that is the rotation angle in degrees, and in axis a vector3 that indicates the direction of the axis.

The relationship between this angle-axis rotation and a quaternion is the following:

1- The w component is the cos(angle/2);

2- The x, y and z components are the axis vector multiplied by sin(angle/2);

Unity could have stored this rotation in a Vector4 structure, indicating the axis in x, y and z, and the angle in w, but they decided to use quaternions because there’s a lot of math developed by crazy mathematicians in the past (more than a century ago!) that perform all the necessary operations in a more efficient manner.

When you multiply two quaternions - A * B, for instance - you actually combine the two rotations: rotate B first, then rotate the result by A - yes, the rotation order is reversed, right to left. You can also rotate a vector by multiplying it by a quaternion - but in the reversed order, as usual with quaternions: Quaternion * Vector3 applies the Quaternion rotation to the Vector3. Thus, if you want to rotate the Vector3.forward vector to a 30 degrees up angle, for instance, you can use this:

``````  Vector3 forwardUp30 = Quaternion.Euler(-30, 0, 0) * Vector3.forward;
``````

NOTE: I used -30 in this case due to the particular orientation of the X rotation - nothing to do with quaternions!

Well, this is a brief explanation about quaternions; you can have more information reading the article Quaternion Powers (not Unity3D related) and a lot of other articles in internet (although most of these were written from a mathematician point of view, what can be lethal to ordinary human beings like us).

This might help:

http://unity3dtutorial.com/unity-3d-tutorials/dealing-with-directions-movement-in-unity-3/

It would be awesome if someone who really understood quaternions and vectors could do a real visual overview of all the functions and concepts involved, but alas, it appears no one has.