Search code examples
androidandroid-sensorsquaternions

How to obtain quaternion from rotation matrix in Android?


I'm trying to obtain the orientation of an android device, and I need it to be in a quaternion structure (float[4] basically).

What I have now is this:

if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
    last_acc = (float[])event.values.clone();
}
else if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
    last_mag = (float[])event.values.clone();
}
if (last_acc != null && last_mag != null){
    SensorManager.getRotationMatrix(rotation, inclination, last_acc, last_mag);
    SensorManager.getOrientation(rotation, orientation);

In "rotation", I have the 4x4 rotation matrix, and in orientation I have a float[3] vector where I have the azimuth, pitch and roll.

How can I get now the quaternion?


Solution

  • I wouldn't use Euler angles (roll, pitch, yaw), it pretty much screws up the stability of your app.

    As for converting the rotation matrix to quaternion, Google says: