tags:

views:

30

answers:

0

Possible Duplicate:
Question about the rotation of X axis on Android

I want to detect the correct rotations around X axis with Android sensors. After googling, I find this code:

public void onSensorChanged(SensorEvent event) {
        Sensor sensor = event.sensor;
        switch(sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            mAcc = event.values.clone();
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            mMag = event.values.clone();
            break;
        }
        if (mAcc == null || mMag == null) return;

        float R[] = new float[9];
        if (SensorManager.getRotationMatrix(R, null, mAcc, mMag)) {
            SensorManager.getOrientation(R, mOrientation);
        }
    }

mOrientation[1] represents the radians around the X axis. However, the value is very odd.

  1. When the phone lies flat top up on the table, it's 0.
  2. When the head of the phone pointing to the ground, it's PI/2.
  3. When the phone lies flat bottom up on the table, it's 0 again.
  4. When the head of the phone pointing to the sky, it -PI/2.

The states between 1,2 have the same rotation values of those between 2,3. How could I tell which state my phone is in?