# Nullify (zero out, cancel) rotation in an arbitrary axis in a Quaternion

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.

• What you've said baffles me a bit. What do you mean "don't rotate the x-axis, just a 45 degree rotation in the y axis"? If you mean that the y axis turns in place 45 degrees, the other two axes must move also... rotations are rigid motions, after all. – rschwieb Mar 28 '13 at 21:46
• Could you possibly be talking about 4-d rotations? Those could be affected by multiplication, as you described, and then I think it is possible to rotate the $y$ axis without moving the $x$ axis. You just have to clear up what your question is. – rschwieb Mar 28 '13 at 21:47
• In 3D each rotation (except the trivial one) is about a single axis, which may be one of the coordinate axes or some other direction in 3-space. The axis of a rotation = the direction that does not move at all. Therefore I fail to understand what you mean by a rotation that is both about the $x$-axes and about the $y$-axes. Do you mean a rotation that has moved the $x$-axis in such a way that after the rotation the angle it forms with the original one is 45 degrees? Ditto for the $y$-axis? – Jyrki Lahtonen Mar 28 '13 at 22:00
• Also, if you want $x$-axis to stay where it is, then $y$-axis is confined to the $yz$-plane, so you cannot select its direction in an arbitrary way. If both $x$ and $y$-axes stay put, then so will the $z$-axis. – Jyrki Lahtonen Mar 28 '13 at 22:02
• Sorry for the confusion. I have heavily edited my question for clarity. Please re-read it. Thank you. – Matthew Alpert Mar 28 '13 at 23:56

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.

• Thank you, but that's not what I was asking. I have heavily edited my question for clarity. Please re-read it. – Matthew Alpert Mar 28 '13 at 23:55
• @MatthewAlpert Thanks, I think that helped a bit, but I still don't think the question is totally clear. You can tell me if I'm catching on o rnot in my comment below your question. – rschwieb Mar 29 '13 at 13:14
• I don't think I have the vocabulary to explain it better then the edited version of my question above. The only other thing I could say is that I'm basically asking for the opposite of your answer. You guessed I would like to pick which axis I rotate around. But what I actually want is to modify a quaternion rotation so that no measurable rotation occurs around a specified axis at all. – Matthew Alpert Mar 30 '13 at 0:16
• @MatthewAlpert In the comment, I suggested something completely different: "Given a rotation q, factor q into rotations about the x,y,z axes: q=rst. By "cancelling out" the x axis rotation, we mean the rotation st (r contained the x-axis rotation)" Does that sound like what you mean? – rschwieb Mar 30 '13 at 11:33
• So you're suggesting decomposing the quaternion rotation into its three Euler rotations, then zeroing out one of the Euler rotations, and finally reconstructing the quaternion? I think that would work, however, it seems like that could only be used to cancel out rotation around one of the cardinal axes. How would you do it for an arbitrary axis? (Though, I do think that decomposing the quaternion is probably a step of the solution. Perhaps it would be useful to convert it to a 3x3, 4x3, or 4x4 matrix representation. But now we're really out of my area of expertise.) – Matthew Alpert Mar 30 '13 at 21:25

From the comments to the other answer: Example: We have a sphere with a dot painted on the surface. A ray from the center of the sphere through the dot points to some fixed point $p$ and defines our axis $r$. We have a Quaternion $q$ that when used to rotate the sphere causes the dot to no longer point at $p$. The question is, how do we modify the Quat $q$ so that the dot still points at $p$? How do we restrict rotation to the axis $r$?

With this statement of your question, I think I can provide a better answer than the other one.

Abusing notation, let $p$ be the position vector of the unrotated dot, and let $p'$ be the position vector of the dot after it has been rotated by $q$. After the rotation $q$ has been performed, the dot will usually not be pointing at $p$ (unless it was on the axis of $q$ to begin with.) Thus $p$ and $p'$ define a plane, for which we can compute a unit normal $h$ and an angle $\theta$ between $p$ and $p'$. When choosing the direction of $h$, do it in such a way that $p',p,h$ forms a right hand system, so that rotating $\theta$ in the plane would carry $p'$ onto $p$.

So we have a new quaternion rotation $z=\cos(\theta/2)+\sin(\theta/2)h$ which performs the last rotation, carrying $p'$ onto $p$. Composing the two rotations into $zq$, you would have a rotation with axis $r$, and so the dot isn't moved.

Finally, it is important to note that if $p$ is perpendicular to the axis of $q$, then $z$ is just going to be $q^{-1}$ in this scheme, and then $zq$ would be the identity. This seems to fit into your question, because in that case the $r$ axis is not experiencing any "torque" during the rotation.

Here's my shot at answering the original question based on the new information. Again, let $p'=qpq^{-1}$, and let $\pi$ be the plane normal to $r$, the direction that you want to cancel rotation in. Project both $p$ and $p'$ into $\pi$, and measure the angle $\theta$ between. Normalize $r$ to $h$ and compute $z$ as before, and then the rotation $zq$ should remove the twist, as you hoped.

The only problem with this is that there are edge cases when $p$ or $p'$ is parallel to $r$, so that the projections don't form vectors. I'll have to think about these tomorrow.

• Very nice. This is similar to my solution for a related problem. I again wanted to align a vector v1 with another vector v2, but v1 could only be rotated around a specified axis (so v1 and v2 might not perfectly align). The solution was to project (v2-v1) onto the plane defined by the desired axis of rotation. Then get the angle and do the rotation. – Matthew Alpert Apr 11 '13 at 4:59

@rschwieb

I believe I've come up with an answer to the original question based on rschwieb's comments and his solution to a similar problem.

Let's say we have some unit direction vector $r$ that defines the axis in which we want no rotation to occur. Pick any unit direction vector $p$ that is perpendicular to $r$.

We also have the Quaternion rotation $q$ from which we want to nullify any rotation in the axis $r$. Rotate $p$ by $q$, calculating $p'=qpq^{*}$ (take care here).

Now, create a plane defined by our axis $r$. Project $p'$ onto that plane creating a new vector $p''$. Be sure to normalize $p''$.

Then, calculate the angle $\theta$ between $p$ and $p''$. $\theta$ is the angle of rotation in the axis $r$ that we want to remove. So, create a Quaternion $z=[cos(\theta/2),sin(\theta/2)r_x,sin(\theta/2)r_y,sin(\theta/2)r_z]$. Be sure to use the correct sign of $\theta$.

Finally, create a new Quaternion $w=zq$. When $w$ is used to rotate an object instead of $q$, no rotation will occur around the axis $r$.

How does that sound?

• The main problem all along was summarized in your earlier comment * I'm starting to think what I'm asking for can't be defined.* Now, the alternative question you posed (about keeping a vector pointing at a point p) was well-defined. The solution I gave for that isolates the component of twisting that the $r$ axis experiences. So, essentially you want to do the opposite: find out how much twisting $r$ experiences and then remove it from the original. – rschwieb Apr 11 '13 at 12:09
• @rschwieb Yes, that's what my original question was. I was simply unable to come up with a scenario to explain it before (to the point where I thought it was impossible). But I think the scenario I've come up with in this answer does address my original question. Do you agree with that? And if so, do you think my answer here works? – Matthew Alpert Apr 12 '13 at 0:35
• There are still problems with this solution. Minorly, $qp$ is not the rotation of the vector $p$: $qpq^{-1}$ is. Majorly, I don't think the angle in this approach is the one you meant to use. The angle of rotation in the plane you described might be different from the angle between $p$ and $p''$, since the plane defined by $p$ and $p''$ probably intersects the plane normal to $r$. I've appended my version of this idea to my solution above. – rschwieb Apr 12 '13 at 2:00
• @rschwieb You're right that I have the vector rotation wrong, but isn't it actually the conjugate, not the inverse, that you're supposed to use ($p'=qpq^{*}$)? As for the angle, I chose $p$ to be a unit vector perpendicular to $r$. So $p$ is in the plane defined by the normal $r$. So when $p'$ is projected into that plane forming $p''$, the arc swept out between $p$ and $p''$ must lie in that plane as well. Thus, the Quaternion $z$ will rotate the object back around $r$, bringing $p$ in line with $p''$. Isn't that right? – Matthew Alpert Apr 24 '13 at 1:48
• Firstly, for every nonzero quaternion $q$, $\frac{q^\ast}{|q|}=q^{-1}$, and in our case, $|q|=1$, causing $q^\ast=q^{-1}$ :) Secondly, I misunderstood that you took $p$ to be perpendicular to $r$ (I was thinking of the more general case where $p$ may not be perpendicular to $r$). If they are perpendicular, I agree. – rschwieb Apr 24 '13 at 13:10