So you’d like to respond programmatically to changes in the spacial orientation of a mobile device. Perhaps you’re writing a feature that directs the user towards a nearby location, or you’d just like to add some surprise and delight parallax to your loading screen.

Landscape layers parallax as the user moves the device.

You might expect the Android OS to send you gyro sensor readings as a set of “Euler Angles”: yaw, pitch, and roll. It doesn’t.

If you don’t typically use 3d graphics APIs and you’d like an intuitive justification for why the Android OS instead gives you a quaternion, read on.

The goal is not to re-hash the math-heavy tutorials you find everywhere online. Rather, we’ll see why 3d applications and frameworks (Unity3d, OpenGL, and countless more) use quaternions. We’ll end with a practical example of translating a quaternion into the familiar Euler angles.

Euler Angles

Oh yeah, before we start I want to be clear about yaw, pitch, and roll.

You might be sitting in something like this:

Spin around like you’re trying to make yourself dizzy. That’s your yaw.

Now rock back and forth like you’re on the porch drinking a lemonade. That’s your pitch.

Now fall over on your side (but don’t hurt yourself). That’s your roll.