I know this has been asked before, but I haven't found the answer I am looking for.
All I want to do is hold my phone up at the sky, and know the angle at which I am holding it with respect to the horizon. So if I am holding it directly above me (screen facing ground) then it should read 90 degrees, and if I hold it in front of me (screen facing me), it should read 0 degrees.
I have fumbled through some basics of getting data from the accelerometer and I believe I have the X, Y and Y values from it. How can I take those points and find my angle? Below is my OnSensorChanged event that I gather the coordinate from.
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
playerAngle = (float) Math.toDegrees(Math.atan2(orientation[1], orientation[0]));
}