Guide to Game Development/Theory/Mathematics/Quaternions

About
Quaternions are used for rotating a geometry and points with along multiple axes of rotation. They exist in four dimensions, and so that have four parts: w, x, y and z. As x, y and z are linked and similar to each other, people sometimes use another letter to represent all three, v is common for this. To represent them you should store them in a 4-dimensional vector like so: $$\textbf{q} = \begin{pmatrix} w \\ x \\ y \\ z \end{pmatrix}$$. Quaternions are used as an alternative to Euler angles.

Benefits of Quaternions :
 * No gimbal locking
 * Interpolation is smooth and direct
 * Simple to do calculations with

Basic creation of a quaternion
So if you chose some unit length 3D-vector that you could like to rotate the geometry around ($$\hat{\textbf{n}}$$) and an amount of degrees that you want to rotate the geometry ($$\theta$$) then create a rotation quaternion ($$\textbf{q}$$).

There are generally two forms for representing quaternion, short and long. In the short notation it shows it in terms of w and v (a combination of x, y and z).

$$\textbf{q} = \bigg(\cos\bigg(\frac{\theta}{2}\bigg), \hat{\textbf{n}}\sin\bigg(\frac{\theta}{2}\bigg)\bigg)$$

This can be represented in the long notation using w, x, y and z:

$$\textbf{q} = \begin{pmatrix} \cos(\frac{\theta}{2}) \\ \hat{\textbf{n}}_x \sin(\frac{\theta}{2})\\ \hat{\textbf{n}}_y \sin(\frac{\theta}{2}) \\ \hat{\textbf{n}}_z \sin(\frac{\theta}{2}) \end{pmatrix}$$

Where : $$w^2+x^2+y^2+z^2=1$$

If a quaternion is not rotated, then it will have the value : $$\textbf{q} = \begin{pmatrix} 1 \\ 0 \\ 0 \\ 0 \end{pmatrix}$$

Links with imaginary numbers
The 4 components (w, x, y and z) can be broken down into one real number and three imaginary numbers:

$$w + ix + jy + kz$$ where $$i$$, $$j$$ and $$k$$ are imaginary numbers such that $$i^2=j^2=k^2=ijk=-1$$. From that, you can create the following statements:

$$ \begin{align} ij=k=-ji \\ jk=i=-kj \\ ki=j=-ik \\ ik=-j=-ki \\ ji=-k=-ij \\ kj=-i=-jk \end{align} $$

Inverse Quaternions
To invert a quaternion, simply multiply the x, y and z components by -1.

$$\textbf{q} = \begin{pmatrix} w \\ x \\ y \\ z \end{pmatrix}$$

$$\textbf{q}^{-1} = \begin{pmatrix} w \\ -x \\ -y \\ -z \end{pmatrix}$$

Multiplying quaternions
When multiplying two quaternions together, order matters ($$\textbf{Q}=\textbf{RS}\neq \textbf{SR}$$).

The multiple is defined by :

$$

\textbf{Q}=\textbf{RS} = [w_r+ix_r+jy_r+kz_r][w_s+ix_s+jy_s+kz_s] = [w_s w_r-\hat{\textbf{n}}_s \cdot \hat{\textbf{n}}_r, w_s\hat{\textbf{n}}_r+w_r\hat{\textbf{n}}_s+\hat{\textbf{n}}_r\times \hat{\textbf{n}}_s] =

\begin{pmatrix}

w_s w_r-\hat{\textbf{n}}_s \cdot \hat{\textbf{n}}_v \\ w_s\hat{\textbf{n}}_{r_x}+w_r\hat{\textbf{n}}_{s_x}+(\hat{\textbf{n}}_r\times \hat{\textbf{n}}_s)_x \\ w_s\hat{\textbf{n}}_{r_y}+w_r\hat{\textbf{n}}_{s_y}+(\hat{\textbf{n}}_r\times \hat{\textbf{n}}_s)_y \\ w_s\hat{\textbf{n}}_{r_z}+w_r\hat{\textbf{n}}_{s_z}+(\hat{\textbf{n}}_r\times \hat{\textbf{n}}_s)_z

\end{pmatrix}

$$

Spherical Linear Interpolation (SLERP)
This is how you Interpolate gradually between two quaternions, like getting a mid-point (or some other point) between two quaternions.

Superscript notation for Quaternions
Before you can do SLERP, you need to understand the superscript notation for a quaternion.

If you have a quaternion a and you use the superscript notation to raise it to the power t, this means that it will scale the angles inside the quaternion by superscript t. As this only scales all of the angles, this means that the magnitude ($$w^2+x^2+y^2+z^2$$) of the Quaternion is still 1.

Superscript notation in short notation
$$\textbf{a} = \bigg(\cos\bigg(\frac{\theta}{2}\bigg), \hat{\textbf{n}}\sin\bigg(\frac{\theta}{2}\bigg)\bigg)$$

$$\textbf{a}^t = \bigg(\cos\bigg(\frac{t\theta}{2}\bigg), \hat{\textbf{n}}\sin\bigg(\frac{t\theta}{2}\bigg)\bigg)$$

Superscript notation in long notation
$$\textbf{a} = \begin{pmatrix} \cos(\frac{\theta}{2}) \\ \hat{\textbf{n}}_x \sin(\frac{\theta}{2})\\ \hat{\textbf{n}}_y \sin(\frac{\theta}{2}) \\ \hat{\textbf{n}}_z \sin(\frac{\theta}{2}) \end{pmatrix}$$

$$\textbf{a}^t = \begin{pmatrix} \cos(\frac{t\theta}{2}) \\ \hat{\textbf{n}}_x \sin(\frac{t\theta}{2})\\ \hat{\textbf{n}}_y \sin(\frac{t\theta}{2}) \\ \hat{\textbf{n}}_z \sin(\frac{t\theta}{2}) \end{pmatrix}$$

The SLERP Equation
If your first Quaternion was q and the second quaternion was r and you wanted to find a point in-between (p) which is t percent of the way from q to r, where 0 ≤ t ≤ 1.

The final equation is defined as :

$$\textbf{p} = (\textbf{r} \textbf{q}^{-1})^t \textbf{q}$$