[Math] Quaternion Rotation

3dquaternionsrotations

I am modelling rotations of a rectangular box (3 dimensions) in Matlab using Quaternion theory.

Using the theory found on https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation I have modelled the following.
Wanting to rotate the box around the $x$- $y$- and $z$-axis I start with three angles,
$\theta_x$, $\theta_y$, $\theta_z$.

To produce pure quaternions from these angles I write,

$q_x = [\; \cos(\theta_x/2)\; \sin(\theta_x/2) \; 0 \; 0\;]'$

$q_y = [\; \cos(\theta_x/2)\; 0 \; \sin(\theta_x/2) \; 0\;]'$

$q_z = [\; \cos(\theta_x/2)\; 0 \; 0 \; \sin(\theta_x/2)\;]'$

With the rotation quaternion about each axis determined I proceed to combine them by calculating the Hamilton Product. Fist combining $q_x$ and $q_y$,

$q_{xy} = \begin{bmatrix} q_y(1)\cdot q_x(1) – q_y(2)\cdot q_x(2) – q_y(3)\cdot q_x(3) – q_y(4)\cdot q_x(4) \\
q_y(1)\cdot q_x(2) + q_y(2)\cdot q_x(1) + q_y(3) \cdot q_x(4) – q_y(4) \cdot q_x(3) \\
q_y(1) \cdot q_x(3) – q_y(2) \cdot q_x(4) + q_y(3) \cdot q_x(1) + q_y(4) \cdot q_x(2) \\
q_y(1) \cdot q_x(4) + q_y(2) \cdot q_x(3) – q_y(3) \cdot q_x(2) + q_y(4) \cdot q_x(1) \end{bmatrix} $

Where 1-4 denotes the respective vector element.

The Hamilton product is applied again on new vector $q_{xy}$ and $q_z$.
Combining all three quaternions can therefore be written,

$q_{xyz} = q_zq_yq_x $

where the Hamilton product is used.

Now back to the original problem: rotating the box.

The box has 8 cornerpoints in total which are described with vectors in the $xyz$-frame. Initially all these have individual values.
Proceeding to apply the quaternion $q_{xyz}$ to each of the corner position vector we calculate,

$c'=q_{xyz}cq_{xyz}'$

where $c'$ is the rotated corner position vector and $q_{xyz}'$ is the quaternion conjugate found by,

$q_{xyz}' = [q_{xyz}(1)\; -q_{xyz}(2)\; -q_{xyz}(3)\; -q_{xyz}(4)]$

And here comes the problem:

If my resulting quaternion $q_{xyz}$ has a rotation that is not \textit{only} along only one of the $xyz$-axis, say some rotation along both the $x$ and $y$-axis, the box does not rotate back to its original position after two opposite rotations (back and forth).
However, if the rotation is only along one axis it does return to its original position, after applying two opposite rotations to each corner position vector.

What is it that I am not understanding about Quaternions rotations since it only works for rotations along one axis at a time in the $xyz$-frame? I want to be able to rotate the box in other directions than along one axis at a time without introducing errors.

Thanks a lot!

Best Answer

If you have unit quaternion $q$ then formula $u \to qu\bar q$ define rotation of imaginary quaternion $u$ over axis $Im(q)$ and angle having cosine of half equal to $Re(q)$. The oposite rotation is obtained by conjugate quaternion $\bar q$ giving formula $u \to \bar quq$. If you combine both transformations you obtain $\bar q qu\bar q q$ which is equal to $u$ because $q$ is unit quaternion and multiplication of quaternions is associative.

If you do not obtain the same point after applying rotation $q$ and $\bar q$ then you must have done some mistake. It is hard to know where might be the mistake without seeing your code.