$\begingroup$

I'm having trouble decomposing a unit quaternion into euler angles (or roll, pitch and yaw). The overall goal is to tell how a phone is rotated with respect to the world. I'm given a unit quaternion this world axis:

The axis has the following characteristics:

X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).

Y is tangential to the ground at the device's current location and points towards magnetic north.

Z points towards the sky and is perpendicular to the ground.

DISCLAIMER: Trying to find the rotation around the z-axis by using atan2(y-component of quaternion, x-component of quaternion) without involving the phone at all, the angle is always between 180 and 360 degrees. Since I'm very new to working with quaternions, I'm trying to figure out whether my error is in the math, or in my code.

I have the following phone axis:

The phone will be held in landscape mode rotated 90 degrees counterclockwise around the z-axis. I need the angle of rotation around the world's z-axis (to see which direction the user is facing), the angle of rotation about the world's y-axis (to determine the angle off of the horizon), and the angle of rotation about the x-axis (to see whether the device is horizontal to the ground).