How should I determine direction from a phone's orientation & accelerometer?
- by Manoj Kumar
I have an Android application which moves a ball based on the orientation of the phone. I've been using the following code to extract the data - but how do I use it to determine what direction the ball should actually travel in?
public void onSensorChanged(int sensor, float[] values) {
// TODO Auto-generated method stub
synchronized (this) {
Log.d("HIIIII :- ", "onSensorChanged: " + sensor + ", x: " + values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor == SensorManager.SENSOR_ORIENTATION) {
System.out.println("Orientation X: " + values[0]);
System.out.println("Orientation Y: " + values[1]);
System.out.println("Orientation Z: " + values[2]);
}
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
System.out.println("Accel X: " + values[0]);
System.out.println("Accel Y: " + values[1]);
System.out.println("Accel Z: " + values[2]);
}
}
}