finding pitch/roll/yaw from Quaternions

I’m using YEI 3-Space Sensor which gives me x,y,z,w Quaternions.
Converting that to Euler angles is done by assigning Quaternions to an object then you can see Euler angles.
I thought I could use Euler format as pitch/roll/yaw, but since Euler is dependent on the sequence those rotations are applied… I now have only “Pitch”(X) since it’s the first applied rotation.

I’ve seen matrices to solve this but isn’t there just a straight forward 1 or 2 equations to solve this?

I think I just need pointing to where i should be looking.

if you have Quaternions ,Euler Angles and pitch.
How would you find pitch/roll/yaw?


I guess I should avoid using Euler angles just because you may face gimbal lock issues.
I solved it using these equations that use Quaternion(x,y,z,w):

roll  = Mathf.Atan2(2*y*w - 2*x*z, 1 - 2*y*y - 2*z*z);
pitch = Mathf.Atan2(2*x*w - 2*y*z, 1 - 2*x*x - 2*z*z);
yaw   =  Mathf.Asin(2*x*y + 2*z*w);


although I have not understood why roll and pitch values range from 3.14 to -3.14 and yaw is 1.52~ to -1.52, but anyway it’s working like a charm !
thanks for the help :slight_smile:

Quaternion q = transform.rotation;
float Pitch = Mathf.Rad2Deg * Mathf.Atan2(2 * q.x * q.w - 2 * q.y * q.z, 1 - 2 * q.x * q.x - 2 * q.z * q.z);
float Yaw = Mathf.Rad2Deg * Mathf.Atan2(2 * q.y * q.w - 2 * q.x * q.z, 1 - 2 * q.y * q.y - 2 * q.z * q.z);
float Roll = Mathf.Rad2Deg * Mathf.Asin(2 * q.x * q.y + 2 * q.z * q.w);

Pitch - yaw - Roll :: X - Y - Z
If you want the values in Radians don’t multiply with Mathf.Rad2Deg

There is a long post with the issues associated with getting pitch/roll/yaw from a Quaternion here:

There is some code at the bottom that works for that programmer’s frame of reference.

Note you do not have to assign a Quaternion to an object to get euler angles. You can use Quaternion.eulerAngles.

since the method below are trans quaternions to eurler angle ,this may cause gimbal lock in some case