How should I determine direction from a phone's orientation & accelerometer?

Posted by Manoj Kumar on Game Development See other posts from Game Development or by Manoj Kumar
Published on 2012-04-13T12:59:10Z Indexed on 2012/04/13 17:43 UTC
Read the original article Hit count: 261

Filed under:
|

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]);
        }            
    }   
}

© Game Development or respective owner

Related posts about java

Related posts about android