# Finding the quaternion that performs a rotation

I managed to find this answer here where Christian Rau says "axis/angle rotation (a,x,y,z) is equal to quaternion (cos(a/2),xsin(a/2),ysin(a/2),z*sin(a/2))"

Assuming I know what rotation I need to perform, how would I represent it? eg, finding the quaternion that rotates 30 degrees around the z axis.

Any help would be greatly appreciated.

edit** I got as far as writing out "(cos(15),0,0," and then got confused on z * sin.

• A rotation about the $z$-axis is a rotation about the unit vector $k$. And we know that $\pi/ 6 = 30^\circ$, but we actually want half of that for the quaternion so $\pi /12$. Therefore the quaternion is $q=e^{\pi k/ 12} = \cos(\pi/ 12) +k\sin(\pi/12)$. Then if you have some vector $v=\alpha i + \beta j + \gamma k$, you just multiply out $qvq^{-1}$ to rotate $v$. – user137731 Oct 27 '15 at 1:26
• That makes sense! :D Thank you @Bye_World! What if you needed to do one rotation followed by another rotation? Like 30 degrees around the y axis followed by 90 around the x axis? Do you just combine them in a single quaternion eg (cos(15*45) + i sin(45) + j sin (15) or do you need to multiply them? – Quiet Oct 27 '15 at 1:44
• You multiply. If your first rotation is represented by the quaternion $q_1$ and your second is $q_2$, then the combined rotation will be $$q_2q_1v{q_1}^{-1}{q_2}^{-1}$$ Remember that $3$D rotations don't commute in general, so make sure you get the order correct. – user137731 Oct 27 '15 at 1:53

To rotate about the $z$ axis (yaw) by $\alpha$ you need the following quaternion

\begin{aligned}q = \begin{bmatrix}\cos(\tfrac{\alpha}{2})\\0\\0\\\sin(\tfrac{\alpha}{2})\end{bmatrix}\end{aligned}\tag{1},

to rotate about the $x$ (pitch) axis you need

\begin{aligned}q = \begin{bmatrix}\cos(\tfrac{\alpha}{2})\\0\\ \sin(\tfrac{\alpha}{2})\\0\end{bmatrix}\end{aligned}\tag{2},

and to rotate about $y$ by $\alpha$ you need

\begin{aligned}q = \begin{bmatrix}\cos(\tfrac{\alpha}{2})\\\sin(\tfrac{\alpha}{2})\\0\\0\end{bmatrix}\end{aligned}\tag{3}.

If you have a rotation described by the Euler angles $(\phi, \theta, \psi)$ (in the standard order), then, the corresponding quaternion is

\begin{aligned} q = \begin{bmatrix} \cos \tfrac{\phi}{2} \cos \tfrac{\theta}{2} \cos \tfrac{\psi}{2} + \sin \tfrac{\phi}{2} \sin \tfrac{\theta}{2} \sin \tfrac{\psi}{2} \\ \sin \tfrac{\phi}{2} \cos \tfrac{\theta}{2} \cos \tfrac{\psi}{2} - \cos \tfrac{\phi}{2} \sin \tfrac{\theta}{2} \sin \tfrac{\psi}{2} \\ \cos \tfrac{\phi}{2} \sin \tfrac{\theta}{2} \cos \tfrac{\psi}{2} + \sin \tfrac{\phi}{2} \cos \tfrac{\theta}{2} \sin \tfrac{\psi}{2} \\ \cos \tfrac{\phi}{2} \cos \tfrac{\theta}{2} \sin \tfrac{\psi}{2} - \sin \tfrac{\phi}{2} \sin \tfrac{\theta}{2} \cos \tfrac{\psi}{2} \end{bmatrix} \end{aligned}\tag{4}.

If you are rotating your object about an axis described by the vector $u=(u_x, u_y, u_z)\in\mathbb{R}^3$ and by an angle $\alpha$ about that axis, then

\begin{aligned}q = \begin{bmatrix}\cos(\tfrac{\alpha}{2})\\\sin(\tfrac{\alpha}{2})u\end{bmatrix}= \begin{bmatrix}\cos(\tfrac{\alpha}{2})\\\sin(\tfrac{\alpha}{2})u_x\\\sin(\tfrac{\alpha}{2})u_y\\\sin(\tfrac{\alpha}{2})u_z\end{bmatrix} \end{aligned} \tag{5}.