Sensor transformation to world coordinate system
suggest changeThe sensor values returned by Android are with respect to the phone's coordinate system (for example, +Y points towards the top of the phone). We can transform these sensor values into a world coordinate system (for example, +Y points towards magnetic North, tangential to the ground) using the sensor manager's rotation matrix. Assigning event.values keeps the sensor's reusable array, so these fields can all end up pointing at the same buffer. getRotationMatrix returns false when it cannot build a matrix, and this sample ignores that result.
First, you would need to declare and initialize the matrices/arrays where data will be stored (you can do this in the onCreate method, for example):
float[] accelerometerData = new float[3];
float[] accelerometerWorldData = new float[3];
float[] gravityData = new float[3];
float[] magneticData = new float[3];
float[] rotationMatrix = new float[9];
Next, we need to detect changes in sensor values, store them into the corresponding arrays (if we want to use them later/elsewhere), then calculate the rotation matrix and resulting transformation into world coordinates:
public void onSensorChanged(SensorEvent event) {
int i = event.sensor.getType();
if (i == Sensor.TYPE_ACCELEROMETER) {
accelerometerData = event.values;
} else if (i == Sensor.TYPE_GRAVITY) {
gravityData = event.values;
} else if (i == Sensor.TYPE_MAGNETIC_FIELD) {
magneticData = event.values;
}
//Calculate rotation matrix from gravity and magnetic sensor data
SensorManager.getRotationMatrix(rotationMatrix, null, gravityData, magneticData);
//World coordinate system transformation for acceleration
accelerometerWorldData[0] = rotationMatrix[0] * accelerometerData[0] + rotationMatrix[1] * accelerometerData[1] + rotationMatrix[2] * accelerometerData[2];
accelerometerWorldData[1] = rotationMatrix[3] * accelerometerData[0] + rotationMatrix[4] * accelerometerData[1] + rotationMatrix[5] * accelerometerData[2];
accelerometerWorldData[2] = rotationMatrix[6] * accelerometerData[0] + rotationMatrix[7] * accelerometerData[1] + rotationMatrix[8] * accelerometerData[2];
}