quaternions - The tool to deal with 3D orientation & rotations

Aris Papasavvas · · ArisDrone

TLDR : This note introduces quaternions as the natural 4D extension of complex numbers for handling 3D rotations, starting from the familiar 2D case. It shows how to build rotation quaternions and apply them to vectors, giving you a clean, mathematically sound way to represent orientations and rotations in 3D space without relying on Euler angles.

Quaternions - The tool to deal with 3D orientation & rotations

Starting point - Getting inspiration from 2D rotations & orientations

In 2D, rotations can be conveniently represented using complex numbers. A rotation of angle $\theta$ applied to a vector around the origin is:

$$\left. \begin{array}{r rl} \text{Rotation } \rightarrow & r = & cos(\theta) + i \cdot sin(\theta) \\ \text{Vector } \rightarrow & v = & v_x + i \cdot v_y \end{array} \right\} \quad \Rightarrow \qquad v' = r \cdot v \quad \leftarrow \quad \text{Rotated vector}.$$

Remark 1: This rotation around the origine is not a rotation around an axis of this 2D space. It’s a rotation around a 3rd axis orthogonal to this 2D space, which could be called $j$. In this new system of coordinats, the vector $v$ we were using is simply : $v = v_x + i \cdot v_y + j \cdot 0.$ From this remark, it makes sens postulating that to work in 3D, we would need a 4th axis which could be called $k$.

New tool - Quaternions and how to use them

Definition of a quaternion - The 4D object useful for 3D rotations & orientations

Let’s introduce the mathematical object called quaternion, noted $q$. It is a 4D object composed of:

  • One scalar part $q_0$,
  • Three imaginary components $\{q_1, q_2, q_3\}$ in an orthonormal basis $(i,j,k)$ $$q = q_0 + i q_1 + j q_2 + k q_3.$$ In this orthonormal basis, the multiplication rules are: $$ii = jj = kk = -1, \qquad ij = -ji = k, \quad jk = -kj = i \quad ki = -ik = j,$$which are consistent with vector cross-product properties in an orthonormal space. One can see: $$i = \begin{pmatrix}1\\0\\0\end{pmatrix}, \quad j = \begin{pmatrix}0\\1\\0\end{pmatrix}, \quad k = \begin{pmatrix}0\\0\\1\end{pmatrix}, \quad i \times j = k \quad \rightarrow \quad ij = k.$$Now, let’s see how this object can be used to do rotations.

How to use quaternions for 3D rotations

Quaternions are 4D objects, with a scalar part and three complex components $(i,j,k)$. For sake of simplicity, we decide that the equivalent of the 3rd axis of the 2D rotations (se Remark 1) is its scalar axis. Therefore, we will use them to apply rotations to 3D vectors $v = [v_x, v_y, v_z]$ written

$$v = i v_x + j v_y + k v_z$$

in their 4D space. Then, to rotate a vector $v$ around a unit axis $n = i\cdot n_x + j \cdot n_y + k \cdot n_z$ by an angle $\theta$ (right-hand rule), we do the following operation:

$$v' = q_r \, v \, \overline{q_r}$$

where:

  • $q_r$ is a rotation quaternion, which is the output of the construction function $f_q$ as follows: $$q_r = f_q(\theta, n) := \cos\left(\frac{\theta}{2}\right) + i \, n_1 \sin\left(\frac{\theta}{2}\right) + j \, n_2 \sin\left(\frac{\theta}{2}\right) + k \, n_3 \sin\left(\frac{\theta}{2}\right),$$
  • $\overline{q_r}$, which is the conjugate of $q_r$, i.e.: $$\overline{q_r} = \cos\left(\frac{\theta}{2}\right) - i \, n_1 \sin\left(\frac{\theta}{2}\right) - j \, n_2 \sin\left(\frac{\theta}{2}\right) - k \, n_3 \sin\left(\frac{\theta}{2}\right) \qquad \big(\text{remark that : }= f_q(-\theta, n)\big).$$ and this equation should be accepted as such since I couldn’t find an intuitive way to explain it. It just works.