[Math] Getting yaw, pitch or roll parts from a quaternion

orientationquaternionsrotations

I have a unit quaternion

q = w + xi + yi + jk

This quaternion means a rotation around an axis. I need to get/extract only one component of this rotation (only yaw, only pitch or only roll, or maybe combined).

But I dont want to convert it to Euler Angles with the common atan2 formulas because I'll make some quaternion multiplications later and I dont want to go back and forth between Quaternion and Euler representations.

What I want is, to get a quaternion that represents only yaw, only pitch and only roll component of the original quaternion.

For example, if a quaternion represents 60, 45, 45 (degree) in Yaw, Pitch, Roll order orientation,
I want a quaternion that is derived from original quaternion that only gives

 60,0,0 for yaw

or

0, 45, 0 (for pitch) and 

or

0, 0, 45 for roll

when converted to Euler angles.

So far, I tried this solution: https://stackoverflow.com/a/5783030/1493265
(If you want to create a quaternion that only rotates around the y axis, you zero out the x and z axes and then re-normalize the quaternion)

But it didn't work the way I need.

Best Answer

Every Quaternion is represented by TWO Euler angles. This is not a well known fact and it's actually one of the biggest problems with Quaternions that basically nobody talks about. Quaternions are assumed to be the catch all for gimbal lock but they have big problems and you need to carefully judge when to use these things and when not to. Simply Using Quaternions all the time for rotations is a HUGE huge mistake.

Assuming the Yaw Pitch Roll convention note that this Euler angle (60, 45, 45) achieves an orientation that can ALSO be realized by (-120, 135, -135). Try mimicking this orientation by turning something according to the order Yaw, Pitch and Roll to develop the intuition for it.

Thus when trying to back convert the Quaternion to a Euler Angle you end up getting TWO possibilities as shown above. You know longer no which one was the original Euler Angle. Many math libraries end up just giving you an answer that's a random choice between the two possibilities.

Note that this secondary Angle is Within a 360 degree sweep. I am not talking about rotating a gimbal 360 degrees back to it's original position. These two angles happen before everything wraps around.

Therefore Quaternions are actually a higher entropy form of Euler Angles. Quaternions HOLD less information then Euler Angles because Quaternions get rid of the physical rotations needed by a gimbal to achieve that orientation.

That's why Quaternions are popular in 3D engines because the physical rotations needed to achieve an orientation are irrelevant. You can get rid of this in 3D games and stuff because it's never needed and it's not important. The 3D engine just needs to draw an object with a certain orientation for every frame... it does not need to rotate an actual gimbal to realize that orientation.

However, for a real world application where you are dealing with a real gimbal YOU should always internally represent your orientation AS a euler angle. Only convert to Quaternion at the end of your calculations when you know that you no longer need the euler angle components.