diff --git a/android-project/src/org/libsdl/app/SDLActivity.java b/android-project/src/org/libsdl/app/SDLActivity.java index 1e194fa3d..02b6ebd61 100644 --- a/android-project/src/org/libsdl/app/SDLActivity.java +++ b/android-project/src/org/libsdl/app/SDLActivity.java @@ -433,6 +433,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, // Sensors protected static SensorManager mSensorManager; + protected static Display mDisplay; // Keep track of the surface size to normalize touch events protected static float mWidth, mHeight; @@ -448,6 +449,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, setOnKeyListener(this); setOnTouchListener(this); + mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); // Some arbitrary defaults to avoid a potential division by zero @@ -621,8 +623,27 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback, @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { - SDLActivity.onNativeAccel(event.values[0] / SensorManager.GRAVITY_EARTH, - event.values[1] / SensorManager.GRAVITY_EARTH, + float x, y; + switch (mDisplay.getRotation()) { + case Surface.ROTATION_90: + x = -event.values[1]; + y = event.values[0]; + break; + case Surface.ROTATION_270: + x = event.values[1]; + y = -event.values[0]; + break; + case Surface.ROTATION_180: + x = -event.values[1]; + y = -event.values[0]; + break; + default: + x = event.values[0]; + y = event.values[1]; + break; + } + SDLActivity.onNativeAccel(x / SensorManager.GRAVITY_EARTH, + y / SensorManager.GRAVITY_EARTH, event.values[2] / SensorManager.GRAVITY_EARTH); } }