Explaining why quaternions work the way they do and their relation to mainstream linear transformation rotations would require some more advanced mathematics knowledge regarding group theory and how it applies to complex numbers and matrices. For your needs however what you need to know is the following:
- Quaternions are 4-dimensional (w,x,y,z) objects with some interesting properties which we can abuse in order to do rotations in 3-dimensional space (x,y,z).
- A 3-dimensional vector or point in space can be represented as a quaternion if we create a quaternion where \$w=0\$ and we set the rest of \$x,y,z\$ to the point/vector coordinates
- The above hacky quaternions have the following property: if a quaternion \$p\$, that represents a point in 3D space, is multiplied by another quaternion and its conjugate in a sandwich \$q*p*q^{-1}\$ a new quaternion results from this operation that also represents a point in 3D space, specifically one that has been rotated by using the following linear transformation matrix $$R=\begin{pmatrix} 1-2y^2-2z^2 & 2xy-2zw & 2xz+2yw \\ 2xy+2zw & 1-2x^2-2z^2 & 2yz-2xw \\ 2xz-2yw & 2yz+2xw & 1-2x^2-2y^2 \end{pmatrix}$$ where the \$w,x,y,z\$ values represent the values of the \$q\$ quaternion.
- The above rotation transformation matrix represents a rotation around the axis represented by \$x,y,z\$ with angle \$2*cos^{-1}(w)\$
To summarize, if you have a 3D point \$x,y,z\$ that you want to rotate by angle \$θ\$ around the axis represented by vector \$[a,b,c]\$ using quaternions, what you need to do is:
- Create a quaternion \$p=0 + xi + yj + zk\$
- Create a quaternion \$q=cos(θ/2) + ai + bj + ck\$
- Do the sandwich multiplication \$p'=q*p*q^{-1}\$
- Extract \$x',y'\$ and \$z'\$ from \$p'\$ and you now have the rotated 3D point \$(x', y', z')\$