I'm not insane - gyroscope swaps axes when the screen orientation is inverted when holding the phone upside down.

This commit is contained in:
pelya
2014-06-29 02:59:58 +03:00
parent 283b1d6b3d
commit 4cd55d4d07
3 changed files with 46 additions and 16 deletions

View File

@@ -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)

View File

@@ -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

View File

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