[Math] Nullify (zero out, cancel) rotation in an arbitrary axis in a Quaternion

3dquaternionsrotations

Question:

How do you nullify (zero out) rotation around an arbitrary axis in a Quaternion?

Example:

Let's say you have an object with quaternion orientation $A$.
You also have a rotation quaternion $B$.
You can calculate a new quaternion orientation $C=BA$.

Let's say the new orientation $C$, if done with axis-angle rotations, is equivalent to a 45 degree rotation around the arrow's local y-axis, followed by a 45 degree rotation around the arrow's local x-axis (which was moved due to the rotation around the y-axis).

In other words, imagine our 3D coordinate frame as a cube. Now imagine the initial orientation $A$ as an arrow in the center of our cube, facing in the negative-z direction. It's pointing at the center of the face of the cube whose normal is in the negative-z direction (with all normals pointing out of the cube).

Now, if we rotate this arrow 45 degrees around its local y-axis, it would be pointing at an edge of the cube. If we rotate it again, this time around its local x-axis, it would then be pointing at a corner of the cube.

The rotations described above can be encoded in a single Quaternion. A specific example of my question is, how can we alter that Quaternion so that it zeros out the rotation around, say, the x-axis, leaving only the rotation around the y-axis (meaning, in the end it would be pointing at an edge of the cube, not the corner)?

My full question is, how can we alter a Quaternion so that it zeros out the rotation around an arbitrary axis?

Remember of course that in general we would not know how the Quaternion was made, meaning we wouldn't know that it represents $a$ degrees around the x-axis, $b$ degrees around the y-axis, and $c$ degrees around the z-axis.

Best Answer

The simplest way to model 3D rotations with quaternions is to view 3D space as the "imaginary quaternions", that is, use $i,j,k$ vectors like you do in physics to model 3D space. (But no real part.)

Every quaternion with norm 1 produces a rotation of this 3D space by conjugation $x\mapsto qxq^{-1}$, where $q$ is any length 1 quaternion.

The formula is surprisingly simple: pick a vector that points along the axis you want to rotate around, and scale it down to a unit vector, call it $h$. Then, take your angle of rotation $\theta$ and form $q=\cos(\theta/2)+h\sin(\theta/2)$. The result is a unit length quaternion that produces the rotation you wanted. ($\theta$ may be positive or negative, depending on the direction of rotation.)

I'm not sure I understand what you mean by "nullify rotation in the x and y axes", but I was guessing you meant you would like to be able to pick which axis you are rotating around.