# QUATERNION ROTATION

Quaternions were first described by Irish mathematician William Rowan Hamilton in 1843 but not widely used until recent innovations with drones and robots.  Euler rotations are easier to visualize but when you string together numerous rotations one axis can become locked.  Quaternions do not have the gimbal lock issue.  The standard formula for quaternion rotation is where Pr is the rotated point:

$Pr=QPQ^{-1}$

Lets start by calculating Q and Q^{-1}.  We will rotate the point (24,0,40) about the x axis vector (1,0,0). The incremental will be 10 degrees so when we click to rotate the x, y or z it will rotate about the current vector by 10 degrees.

$Q=a+b\vec{i}+c\vec{j}+d\vec{k}$

$Q=\cos(\frac{\frac{10*\pi}{180}}{2})+sin(\frac{\frac{10*\pi}{180}}{2})*1+sin(\frac{\frac{10*\pi}{180}}{2})*0+sin(\frac{\frac{10*\pi}{180}}{2})*0$

$Q=0.9962+0.0871\vec{i}+0\vec{j}+0\vec{k}$

$Q^{-1}=a+b\vec{i}+c\vec{j}+d\vec{k}$

$Q^{-1}=\cos(-\frac{\frac{10*\pi}{180}}{2})+sin(-\frac{\frac{10*\pi}{180}}{2})*1+sin(-\frac{\frac{10*\pi}{180}}{2})*0+sin(-\frac{\frac{10*\pi}{180}}{2})*0$

$Q^{-1}=0.9962-0.0871\vec{i}+0\vec{j}+0\vec{k}$

The next step is to calculate the Hamilton Product of QP.

$(a_1+b_1vec{i}+c_1vec{j}+d_1\vec{k})(a_2+b_2vec{i}+c_2vec{j}+d_2\vec{k})=$

$a_1a_2+a_1b_2\vec{i}+a_1c_2\vec{j}+a_1d_2\vec{k}$

$+b_1a_2\vec{i}+b_1b_2\vec{i^2}+b_1c_2\vec{i}\vec{j}+b_1d_2\vec{i}\vec{k}$

$+c_1a_2\vec{j}+c_1b_2\vec{j}\vec{i}+c_1c_2\vec{j^2}+c_1d_2\vec{j}\vec{k}$

$+d_1a_2\vec{k}+d_1b_2\vec{k}\vec{i}+d_1c_2\vec{k}\vec{j}+d_1d_2\vec{k^2}$

After multiplying the basis elements you get the following:

$a_1a_2-b_1b_2-c_1c_2-d_1d_2$

$+(a_1b_2+b_1a_2+c_1d_2-d_1c_2)\vec{i}$

$+(a_1c_2-b_1d_2+c_1a_2+d_1b_2)\vec{j}$

$+(a_1d_2+b_1c_2-c_1b_2+d_1a_2)\vec{k}$

Now we can substitute our values to calculate QP:

$QP=\begin{pmatrix} (.9962*0-.0871*24-0*0-0*40 )\\ (.9962*24+.0871*0+0*40-0*0)\vec{i}\\(.9962*0-.0871*40+0*0+0*24)\vec{j}\\(.9962*40+.0871*0-0*24+0*0)\vec{k} \end{pmatrix}$

$QP=\begin{pmatrix} -2.092\\ 23.9087\vec{i}\\-3.4862\vec{j}\\39.8478\vec{k} \end{pmatrix}$

$QPQ^{-1}=\begin{pmatrix}(-2.092*-.0871+23.9087*.9962+-3.4862*0-39.8478*0)\vec{i}\\(-2.092*0-23.9087*0+-3.4862*.9962+39.8478*-.0871)\vec{j}\\(-2.092*0+23.9087*0+3.4862*-.0871+39.8478*.9962)\vec{k}\end{pmatrix}$

$QPQ^{-1}=\begin{pmatrix}24\vec{i}\\-6.945\vec{j}\\39.392\vec{k}\end{pmatrix}$

The point (24,0,40) will move to (24,-6.945,39.392) when rotated 10 degrees about the x axis.  Click here to download my excel sheet with quaternion rotation of a manhole.