# Compute relative orientation given azimuth, pitch, and roll in android?

When I listen to orientation event in an android app, I get a SensorEvent, which contains 3 floats - azimuth, pitch, and roll in relation to the real-world's axis.

Now say I am building an app like labyrinth, but I don't want to force the user the be over the phone and hold the phone such that the xy plane is parallel to the ground. Instead I want to be able to allow the user to hold the phone as they wish, laying down or, perhaps, sitting down and holding the phone at an angle. In other words, I need to calibrate the phone in accordance with the user's preference.

How can I do that?

Also note that I believe that my answer has to do with getRotationMatrix and getOrientation, but I am not sure how!

Please help! I've been stuck at this for hours.

## Answers

For a Labyrinth style app, you probably care more for the acceleration (gravity) vector than the axes orientation. This vector, in Phone coordinate system, is given by the combination of the three accelerometers measurements, rather than the rotation angles. Specifically, only the **x** and **y** readings should affect the ball's motion.

If you do actually need the orientation, then the 3 angular readings represent the 3 Euler angles. However, I suspect you probably don't really need the angles themselves, but rather the rotation matrix **R**, which is returned by the **getRotationMatrix()** API. Once you have this matrix, then it **is** basically the calibration that you are looking for. When you want to transform a vector in world coordinates to your device coordinates, you should multiply it by the *inverse* of this matrix (where in this special case, inv(**R**) = transpose(**R**).

So, following the example I found in the documentation, if you want to transform the world gravity vector *g* ([0 0 g]) to the device coordinates, multiply it by inv(**R**):

**g** = inv(**R**) * *g*

(note that this should give you the same result as reading the accelerometers)

Possible APIs to use here: **invertM()** and **multiplyMV()** methods of the matrix class.