Saturday, September 17, 2016

Matrix Representation of Quaternion Rotation


Quaternions are an amazing tool for performing rotations in 3-space. Every game engine worth of the name has a module for computing with quaternions. Internally, rotations are still performed using matrices but instead of composing rotations by multiplying matrices together, quaternions are multiplied. The final resulting quaternion is then converted into a rotation matrix which is fed to the game engine for further processing. This final step is very important yet many game programmers tend to use the conversion routines as a black box, which is okay if you are not interested in the mathematics; but understanding the mathematics can make a difference.

I am going to assume that you are familiar with quaternion multiplication and that you know the quaternion rotation formula. Here is the formula for reference:

$$rot(v,n,\theta)=qvq^*.$$

Here $q=\exp(\frac{\theta}{2}n)=\cos(\frac{\theta}{2})+ \sin(\frac{\theta}{2})n_xi + \sin(\frac{\theta}{2})n_yj + \sin(\frac{\theta}{2})n_zk$, where $\theta$ is the rotation angle in radians; $n$ is the unit rotation vector, $n=n_xi + n_yj + n_zk$; and $v$ is the vector we want to rotate. $q^*$ is notation for the conjugate of $q$: $q^*=\cos(\frac{\theta}{2})- \sin(\frac{\theta}{2})n_xi - \sin(\frac{\theta}{2})n_yj - \sin(\frac{\theta}{2})n_zk$.

From Quaternion to Matrix

The formula for rotation involves quaternion multiplication and we are going to use that to represent the multiplication in the matrix world. So let us say that we are given two quaternions $p=a+bi+cj+dk$ and $q=e+fi+gj +hk$. Note that we can think of a quaternion as being a 4 dimensional column vector: $p$ can be represented as the column vector $\left( \begin{matrix} a & b & c & d \end{matrix} \right)^T$. Let us begin by multiplying the quaternions $p$ and $q$.

$$pq=(a+bi+cj+dk)(e+fi+gj +hk).$$

Recall that the rules for quaternion multiplication are: $i^2=j^2=k^2=ijk=-1$.

Now multiplying and collecting terms gives:

$$\begin{align} pq= & (ae-bf-cg-dh)+ \\ & (be+af-dg+ch)i+ \\ & (ce+df+ag-bh)j+ \\ & (de-cf+bg+ah)k  \end{align}$$

I laid out the product above in that way for a reason. If you know some linear algebra then you can tell how to represent the product above as a matrix-vector product just by looking at it. The first column has $e$'s in it, the second column has $f$'s in it, etc.. The product is crying out to be written in matrix form.

$$\left( \begin{matrix} a & -b & -c & -d \\ b & a & -d & c \\ c & d & a & -b \\ d & -c & b & a \end{matrix} \right) \left( \begin{matrix} e \\ f \\ g \\ h \end{matrix}\right)$$.

That's how you represent a product of two quaternions as a matrix-vector product. In the above matrix the quaternion $q$ is being used as a vector; can we use $p$ as a vector instead ? Yes, we just rearrange the terms.

$$\begin{align} pq= & (ae-bf-cg-dh)+ \\ & (af+be+ch-dg)i+ \\ & (ag-bh+ce+df)j+ \\ & (ah+bg-cf+de)k  \end{align}$$

This gives the following matrix-vector product.

$$\left( \begin{matrix} e & -f & -g & -h \\ f & e & h & -g \\ g & -h & e & f \\ h & g & -f & e \end{matrix} \right) \left( \begin{matrix} a \\ b \\ c \\ d \end{matrix}\right)$$.

That was not very hard. Quaternion multiplication is not commutative so the matrices above correspond to multiplication on the left and on the right. Let us now turn to the problem of expressing quaternion rotations as multiplication by matrices.

From Rotation Quaternion to Rotation Matrix

Now that we know how to represent quaternion multiplication as a matrix-vector product, we are going to use that knowledge to derive a formula to represent quaternion rotation as multiplication by a matrix. There seems to be a lot of mystery surrounding this on the internet; people who write about this give the formula for the reader to use as a black box and they don't worry about giving details about where it came from.

Look at the formula for quaternion rotation. We first multiply $v$ by the quaternion $q$ on the left and then we multiply the result by the quaternion $q^*$ on the right (or you can think of it as multiplying $v$ by $q^*$ on the right and then multiplying the result by $q$ on the left). Now call the matrix corresponding to multiplication by $q$ on the left $A$ and call the matrix corresponding to multiplication by $q^*$ on the right $B$. We first represent the product $qv$ as $Av^T$. Now we have to include $q^*$ in the product but we are multiplying on the right so the matrix product gives $BAv^T$ (here $v^T$ is the vector representation of the quaternion $v$). That's it, we have our matrix representation of quaternion rotation. In fact, $ABv^T$ gives the same effect; it depends on which multiplication you do first: $qv$ or $vq^*$. So this tells us that $A$ and $B$ commute in this case: $AB=BA$. Now let us go ahead and compute this product.

$$AB=\left( \begin{matrix} a & -b & -c & -d \\ b & a & -d & c \\ c & d & a & -b \\ d & -c & b & a \end{matrix} \right) \left( \begin{matrix} a & b & c & d \\ -b & a & -d & c \\ -c & d & a & -b \\ -d & -c & b & a \end{matrix} \right)$$

$$=\left( \begin{matrix} a^2+b^2+c^2+d^2 & 0 & 0 & 0 \\ 0 & a^2+b^2-c^2-d^2 & 2(bc-ad) & 2(ac+bd) \\ 0 & 2(ad+bc) & a^2-b^2+c^2-d^2 & 2(cd-ab) \\ 0 & 2(bd-ac) & 2(ab+cd) & a^2-b^2-c^2+d^2 \end{matrix} \right)$$

I will let you compute $BA$ to make sure that it gives the same result. One last thing to mention is that sometimes people change the order of the components in a quaternion; this will change the matrix but the terms will remain the same.

I hope that this will end up being useful to you. Feel free to drop a comment or ask a question.