Example #1
0
 @Override
 public void onSensorChanged(SensorEvent event) {
   switch (event.sensor
       .getType()) { //  both accelerometer and magnetic data are needed to compute orientation
     case Sensor.TYPE_ACCELEROMETER:
       System.arraycopy(event.values, 0, accelValues, 0, 3);
       if (compassValues[0] != 0) sensorsReady = true;
       break;
     case Sensor.TYPE_MAGNETIC_FIELD:
       System.arraycopy(event.values, 0, compassValues, 0, 3);
       if (accelValues[2] != 0) sensorsReady = true;
       break;
     default:
       break;
   }
   if (sensorsReady
       && SensorManager.getRotationMatrix(inR, inclineMatrix, accelValues, compassValues)) {
     SensorManager.getOrientation(inR, prefValues);
     pitch = (float) Math.toDegrees(prefValues[1]);
     roll = (float) Math.toDegrees(prefValues[2]);
     if (mc != null) mc.updateOrientation(pitch, roll); // Update character's position
   }
 }