# Representing rotations using quaternions

I'm learning Unity and came across a situation where rotations are represented as Quaternions. I've heard that they where used in computer graphics, but never had to use them until now. What I can't understand is, how do quaternions represents rotations in the three dimensional space?

I know that complex numbers represents rotations in the 2 dimensional space (such as multiplying a number by $i$ would move it by $\frac \pi 2$). This seems logical in the sense that $\Bbb C$ has two unit vectors $i$ and $1$, like the two dimensional space. Why would we need a four dimensional set to represent rotations on a three dimensional space? And what kind of rotation would a quaternion (for example $i+j+k+1$) represent?

Remark:

On the tutorial I'm following to learn Unity, I've used the Quaternion.Euler(float x, float y, float z) function to create a rotation. From the documentation about the later:

Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

Note that the fourth dimension isn't used to define that rotation.

• I know it's not relavant, but I assume you are making 3D fractals? – AvZ Mar 4 '15 at 15:36
• @AvZ Not at all, I was just trying to create some animations in a game. – moray95 Mar 4 '15 at 15:37
• Now you've sparked my interest. How are you making them using quaternions? – AvZ Mar 4 '15 at 15:38
• @AvZ Haha, it's kind of a broad subject but as I wrote in the question, I'm using a game engine called Unity and C# language. The rotation animation I asked about is explained in this tutorial around 19:00. – moray95 Mar 4 '15 at 15:47
• There's a fairly detailed answer on Wikipedia in the article on SO(3). And even more detail in Charts on SO(3) § Parametrizations. And even a full article: Quaternions and spatial rotation . The standard graphics textbook answer it that quaternions avoid gimbal lock. – Fizz Mar 4 '15 at 15:57

## 3 Answers

The key fact is that:

a rotation of an angle $2\theta$ in space, around an axis passing through the origin, is represented by a quaternion $e^{\mathbf{u}\theta}$, where $\mathbf{u}$ is the imaginary quaternion that correspond to the unit vector oriented along the axis of rotation. So we have the correspondence: $$\vec{w}=R_{\mathbf{u},\theta} \; \vec{v} \quad \longleftrightarrow \quad \mathbf{w}= e^{\mathbf{u}\theta/2}\mathbf{v}e^{-\mathbf{u}\theta/2}$$

See my answer to :Quaternions vs Axis angle.

For the exponentiaton we have that if $\mathbf{v} \in \mathbb{H}_P$ is an imaginary quaternion, putting $\theta=|\mathbf{v}|$ we have: $$e^\mathbf{v}= \cos\theta + \mathbf{v}\;\dfrac{\sin \theta}{\theta}$$

Why would we need a four dimensional set to represent rotations on a three dimensional space?

Short answer: We don't. A rotation quaternion only has three degrees of freedom, because it is limited to unit length: $$|Q| = w^2 + x^2 + y^2 + z^2 = 1$$

Longer answer: If you have any three of those, you can easily calculate the fourth whenever you need it. But if you do that, you lose the possibility of measuring and correcting accumulated errors caused by the limited precision used to represent those numbers in a computer.

After any manipulation of the quaternion, the limited precision may have caused the square sum to deviate somewhat from 1. This deviation gives an estimate of the precision you have lost.

To regain most of that precision, you can multiply each element by the inverse of the length: $$Q_2 = Q_1 * \sqrt{\frac{1}{w^2 + x^2 + y^2 + z^2}}$$ Thus regaining the unity property by repairing your quaternion as best you can.

Tip for extra high precision: Calculate a re-normalized quaternion after every manipulation. Keep the new one only if its length is closer to 1 than the old one.

Also, don't use float but double.

"Why would we need a four dimensional set to represent rotations on a three dimensional space?"

"These articles are too technical for me to understand"

You can pick an axis and do a complex-number-type rotation around that axis. If you do that repeatedly, you may sometimes find that the axis you need to rotate around is the direction of the vector you want to rotate. "Gymbal lock".

Quaternions with four dimensions do two complex rotations at the same time, by the same number of degrees. One of the rotations is perpendicular to the direction of the vector part of the quaternion, and the scalar part of a unit quaternion is like the cosine of the angle you rotate through. That's the rotation you want. BUT at the same time, you get a second complex rotation between the axis dimension and the scalar dimension. Your result is not a vector but a quaternion.

It turns out that you can multiply on the other side to get the rotation you want, but the second rotation is in the opposite direction. So rotate half the angle you want, then on the other side rotate the other half and cancel out the part you don't want. No gymbal lock. You can get some estimate of your rounding error by checking how much the vector length has changed.