From 4cd55d4d07bac71bf70e8be79e8b02faf66576a6 Mon Sep 17 00:00:00 2001 From: pelya Date: Sun, 29 Jun 2014 02:59:58 +0300 Subject: [PATCH] I'm not insane - gyroscope swaps axes when the screen orientation is inverted when holding the phone upside down. --- project/java/Accelerometer.java | 31 ++++++++++++++++++------------- project/java/MainActivity.java | 20 +++++++++++++++++--- project/java/Video.java | 11 +++++++++++ 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/project/java/Accelerometer.java b/project/java/Accelerometer.java index e99043b76..90d19ec22 100644 --- a/project/java/Accelerometer.java +++ b/project/java/Accelerometer.java @@ -90,25 +90,30 @@ class AccelerometerReader implements SensorEventListener static class GyroscopeListener implements SensorEventListener { public float x1 = 0.0f, x2 = 0.0f, xc = 0.0f, y1 = 0.0f, y2 = 0.0f, yc = 0.0f, z1 = 0.0f, z2 = 0.0f, zc = 0.0f; + public boolean invertedOrientation = false; public GyroscopeListener() { } public void onSensorChanged(SensorEvent event) { - // TODO: vertical orientation - if( Globals.HorizontalOrientation ) + if( event.values[0] < x1 || event.values[0] > x2 || + event.values[1] < y1 || event.values[1] > y2 || + event.values[2] < z1 || event.values[2] > z2 ) { - if( event.values[0] < x1 || event.values[0] > x2 || - event.values[1] < y1 || event.values[1] > y2 || - event.values[2] < z1 || event.values[2] > z2 ) - nativeGyroscope(event.values[0] - xc, event.values[1] - yc, event.values[2] - zc); - } - else - { - if( event.values[0] < x1 || event.values[0] > x2 || - event.values[1] < y1 || event.values[1] > y2 || - event.values[2] < z1 || event.values[2] > z2 ) - nativeGyroscope(-(event.values[1] - yc), event.values[0] - xc, event.values[2] - zc); + if( Globals.HorizontalOrientation ) + { + if( invertedOrientation ) + nativeGyroscope(-(event.values[0] - xc), -(event.values[1] - yc), event.values[2] - zc); + else + nativeGyroscope(event.values[0] - xc, event.values[1] - yc, event.values[2] - zc); + } + else + { + if( invertedOrientation ) + nativeGyroscope(event.values[1] - yc, -(event.values[0] - xc), event.values[2] - zc); + else + nativeGyroscope(-(event.values[1] - yc), event.values[0] - xc, event.values[2] - zc); + } } } public void onAccuracyChanged(Sensor s, int a) diff --git a/project/java/MainActivity.java b/project/java/MainActivity.java index c43df63c1..ce82058b0 100644 --- a/project/java/MainActivity.java +++ b/project/java/MainActivity.java @@ -79,6 +79,8 @@ import android.content.pm.ActivityInfo; import android.view.Display; import android.text.InputType; import android.util.Log; +import android.view.Surface; + public class MainActivity extends Activity { @@ -231,7 +233,7 @@ public class MainActivity extends Activity if( Parent._tv == null ) { //Get the display so we can know the screen size - Display display = getWindowManager().getDefaultDisplay(); + Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); Parent._tv = new TextView(Parent); @@ -266,6 +268,8 @@ public class MainActivity extends Activity public void initSDL() { setScreenOrientation(); + updateScreenOrientation(); + Log.i("SDL", "onConfigurationChanged(): screen orientation: inverted " + AccelerometerReader.gyro.invertedOrientation); (new Thread(new Runnable() { public void run() @@ -836,10 +840,20 @@ public class MainActivity extends Activity @Override public void onConfigurationChanged(Configuration newConfig) { + // This function is actually never called super.onConfigurationChanged(newConfig); - // Do nothing here + updateScreenOrientation(); } - + + public void updateScreenOrientation() + { + int rotation = Surface.ROTATION_0; + if( android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.FROYO ) + rotation = getWindowManager().getDefaultDisplay().getRotation(); + AccelerometerReader.gyro.invertedOrientation = ( rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270 ); + //Log.d("SDL", "updateScreenOrientation(): screen orientation: " + rotation + " inverted " + AccelerometerReader.gyro.invertedOrientation); + } + public void setText(final String t) { class Callback implements Runnable diff --git a/project/java/Video.java b/project/java/Video.java index f7ef28034..e19b970fd 100644 --- a/project/java/Video.java +++ b/project/java/Video.java @@ -719,6 +719,16 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer Thread.sleep(50); // Give some time to the keyboard input thread } catch(Exception e) { }; } + + // We will not receive onConfigurationChanged() inside MainActivity with SCREEN_ORIENTATION_SENSOR_LANDSCAPE + // so we need to create a hacky frame counter to update screen orientation, because this call takes up some time + mOrientationFrameHackyCounter++; + if( mOrientationFrameHackyCounter > 100 ) + { + mOrientationFrameHackyCounter = 0; + context.updateScreenOrientation(); + } + return 1; } @@ -913,6 +923,7 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer public int mWidth = 0; public int mHeight = 0; private ClipboardManager clipboard = null; + int mOrientationFrameHackyCounter = 0; public static final boolean mRatelimitTouchEvents = true; //(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO); }