• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How do I detect rotation around an axis and keep the co-ordinate system fixed to the device?

Highway62

Lurker
I'm trying to detect motion around the z-axis, but I need the co-ordinate system to be fixed to the device instead of the world, so that no matter what orientation the phone is, I am still detecting rotation around the z-axis that is pointing straight out of the phone screen.

The sensor I am using just now is the TYPE_GAME_ROTATION_VECTOR like this:

Code:
public void onSensorChanged(SensorEvent event) {

     if (event.sensor.getType() == Sensor.TYPE_GAME_ROTATION_VECTOR) {
         mGameRotation = event.values;
     }


     if (mGameRotation != null) {
         float R[] = new float[9];
         SensorManager.getRotationMatrixFromVector(R, mGameRotation);
         float orientation[] = new float[3];
         SensorManager.getOrientation(R, orientation);
         zDegrees = Math.toDegrees(orientation[0]);
         textZ.setText("Z-Axis: " + String.format("%.2f", zDegrees));
    }
}

Here I am capturing the z-axis rotation and outputting it to my text view. However, when the phone rotates it picks up the z-axis rotation as expected, but the Z value also changes when I move the phone from lying on a table to vertical in my hand (x-axis), or when I hold the phone vertical and spin around in my chair (y-axis), or even if I hold the phone vertical and move it directly up or down, with no rotation at all! How can I set it so that the only time the z-axis value changes is when the phone is rotating around the z-axis, and the z-axis is fixed so that orientation doesn't matter? Thanks
 
Back
Top Bottom