Combining rotation quaternions

To rotate a vector $v = ix + jy + kz$ by a quaternion $q$ you compute $v^q = q v q^{-1} $.

So if $q$ and $q'$ are two rotation quaternions, to rotate by $q$ then $q'$ you calculate $(v^q)^{q'} = q' q \,v\, q^{-1} q'^{-1} = q' q \,v\, (q' q)^{-1} = v^{q'q}.$


References

Quaternions and spatial rotation


If you check some of the resources in the earlier question you'll find that the most useful way quaternions act as rotations is by conjugation.

Think of the $i,j,k$ vectors as orthonormal vectors in 3-dimensional space, as we usually do in physics. Every point in 3-space then is just a linear combination of these three vectors. These are the "pure quaternions" whose real parts are 0.

Given a quaternion with norm 1, call it $u$, you can rotate a pure quaternions $v$ by conjugating: $v\mapsto uvu^{-1}$. Let $w$ be another quaternion with norm 1. Then as you observed, you can rotate by $u$ and $w$ in two different orders:

$$wuvu^{-1}w^{-1}=(wu)v(wu)^{-1}$$

or

$$uwvw^{-1}u^{-1}=(uw)v(uw)^{-1}$$

which potentially can be different.

Let's try it with a few very simple choices of $u$ and $w$. Try $u=i$ and $w=j$, and see what happens to the $i,j,k$ vectors under those rotations. If we try this with $u=i$, you can check that $$i\mapsto iii^{-1}=i$$ $$j\mapsto iji^{-1}=-j$$ $$k\mapsto iki^{-1}=-k$$.

Visualize what has happened to the original triad $i,j,k$ after rotation. I'll leave the other example to you.

To customize length 1 quaternions that rotate things the way you want to, you'll have to take a look at the wiki article. Basically the idea is this: every rotation in 3-space is specified by an axis of rotation and the angle you rotate about that axis. To find your customized $u$, you first compute a unit quaternion $h$ which is normal to the plane of rotation, and then an expression like $u=\cos(\theta/2)+h\sin(\theta/2)$ turns out to be what you want. (I haven't been careful about specifying the direction and rotation or signs in this sketch, so take care when following the detailed explanation.)