Skip to content
UoL CS Notes

Quaternions and 3D Graphics

COMP116 Lectures

How to we extend the complex plane to 3D? We do this by considering vector algebra with four components.

Quaternion Structure

Complex numbers can be written as 2-vectors $\langle x,y\rangle$.

Quaternions use points in $\Bbb R^4$ (all values are real) of the form:

\[q=(w,x,y,z)\]

Matching $z=x+iy$ we now have:

\[q=w+ix+jy+kz\]

Where: \(i^2=j^2=k^2=-1\)

And:

\[\begin{aligned} jk&=-kj=i\\ ki&=-ik=j\\ ij&=-ji=k \end{aligned}\]

Notation

  • $\Bbb H$ - The set of all quaternions.

    The notation $\Bbb H$ is in honour of William Hamilton

  • $q\in\Bbb H$ - An arbitrary quaternion.
  • The forms $q=w+ix+jy+kz$ and $q[\alpha,\vec v]$ are both used.
    • In the second of these $w\equiv \alpha \in \Bbb R;$ $\vec v=\langle x,y,.z\rangle\in\Bbb R^3$

      The vector $\vec v$ is a 3-vector in $\Bbb R$.

Operations

  • Addition
    • Add the corresponding components.
  • Scalar Multiplication by $\alpha\in\Bbb R$
    • Multiply individual components by $\alpha$.
  • Conjugate
    • If $q=(w,x,y,z)$, $\bar q=(w,-x,-y,-z)$
    • If $q=[\alpha,\vec v]$, $\bar q=[\alpha,-\vec v]$
  • Modulus
    • $\vert\vert(w,x,y,z)\vert\vert=\sqrt{w^2+x^2+y^2+z^2}$
    • Equivalently $\vert\vert[\alpha,\vec v]\vert\vert=\sqrt{\alpha^2+\vert\vert\vec v\vert\vert^2}$
    • If $\vert\vert q\vert\vert=1$, $q$ is called a unit quaternion.
  • Division
    • $q^{-1}=\frac{\bar q}{\vert\vert q\vert\vert^2}$

Multiplication

For two arbitrary quaternions $s,t\in\Bbb H$ how do we form their product?

Expanding brackets is complicated and tedious. Consider this form, for $s=[\alpha,\vec u]$ and $t=[\beta,\vec v]$, $s\times t=$:

\[[\alpha\beta-\vec u\cdot\vec v,\alpha\vec v+\beta\vec u+\vec u\times\vec v]\]

Pay attention to the dot product and the cross product.

Quaternion product is not commutative ($s\cdot t\neq t\cdot s$)

3D Graphics

Matrix-vector products don’t work well for rotation in 3D. Additionally they are computationally expensive.

Gimbal Lock

This phenomenon occurs when using matrix-vectors for 3D graphics. This is because you may ask for undefined values of trig functions. This will occur in errors.

Quaternions do not suffer from these errors.

Rotation with Quaternions

Suppose we are looking to rotate a point $(x,y,z)$ about some axis of rotation (an infinite line in 3D) $\vec v=\langle \alpha,\beta,\gamma\rangle$.

We wish to rotate through some angle $\theta^\circ$.

  1. Choose the quaternion $q_\theta=[\cos(\frac{\theta}{2}),\sin(\frac{\theta}{2})\vec v]$

    Since we are dealing with a line we can always choose $\vec v$ so that $q_\theta$ is a unit quaternion.

  2. The computation “rotate $\vec w=\langle x,y,z\rangle$ by $\theta^\circ$ about $\vec v$” is just the quaternion product:

    \[q_\theta\cdot[0,\vec w]\cdot q_\theta^{-1}=q_\theta^{-1}\cdot[0,\vec w]\cdot q_\theta\]