diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-12-15 20:25:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-15 20:25:59 +0100 |
commit | e311389580226f0fc5adb6abe785d7af58e06892 (patch) | |
tree | 481fffbd97c79d427b2c615cbe45061704609c25 | |
parent | deb30a4108f05f641faae87afab510e118822d6c (diff) | |
parent | 6e38fc4b80982d8da370758d1873e94613267d5c (diff) |
Merge pull request #55183 from madmiraal/fix-41421
Only rotate Android sensor values for sensors that need them rotated
-rw-r--r-- | platform/android/java/lib/src/org/godotengine/godot/Godot.java | 95 |
1 files changed, 64 insertions, 31 deletions
diff --git a/platform/android/java/lib/src/org/godotengine/godot/Godot.java b/platform/android/java/lib/src/org/godotengine/godot/Godot.java index 17ff3c75c0..2fecf2e6ba 100644 --- a/platform/android/java/lib/src/org/godotengine/godot/Godot.java +++ b/platform/android/java/lib/src/org/godotengine/godot/Godot.java @@ -727,45 +727,78 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC }); } - @Override - public void onSensorChanged(SensorEvent event) { + public float[] getRotatedValues(float values[]) { + if (values == null || values.length != 3) { + return values; + } + Display display = ((WindowManager)getActivity().getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); int displayRotation = display.getRotation(); - float[] adjustedValues = new float[3]; - final int[][] axisSwap = { - { 1, -1, 0, 1 }, // ROTATION_0 - { -1, -1, 1, 0 }, // ROTATION_90 - { -1, 1, 0, 1 }, // ROTATION_180 - { 1, 1, 1, 0 } - }; // ROTATION_270 + float[] rotatedValues = new float[3]; + switch (displayRotation) { + case Surface.ROTATION_0: + rotatedValues[0] = values[0]; + rotatedValues[1] = values[1]; + rotatedValues[2] = values[2]; + break; + case Surface.ROTATION_90: + rotatedValues[0] = -values[1]; + rotatedValues[1] = values[0]; + rotatedValues[2] = values[2]; + break; + case Surface.ROTATION_180: + rotatedValues[0] = -values[0]; + rotatedValues[1] = -values[1]; + rotatedValues[2] = values[2]; + break; + case Surface.ROTATION_270: + rotatedValues[0] = values[1]; + rotatedValues[1] = -values[0]; + rotatedValues[2] = values[2]; + break; + } - final int[] as = axisSwap[displayRotation]; - adjustedValues[0] = (float)as[0] * event.values[as[2]]; - adjustedValues[1] = (float)as[1] * event.values[as[3]]; - adjustedValues[2] = event.values[2]; + return rotatedValues; + } - final float x = adjustedValues[0]; - final float y = adjustedValues[1]; - final float z = adjustedValues[2]; + @Override + public void onSensorChanged(SensorEvent event) { + if (mRenderView == null) { + return; + } final int typeOfSensor = event.sensor.getType(); - if (mRenderView != null) { - mRenderView.queueOnRenderThread(() -> { - if (typeOfSensor == Sensor.TYPE_ACCELEROMETER) { - GodotLib.accelerometer(-x, y, -z); - } - if (typeOfSensor == Sensor.TYPE_GRAVITY) { - GodotLib.gravity(-x, y, -z); - } - if (typeOfSensor == Sensor.TYPE_MAGNETIC_FIELD) { - GodotLib.magnetometer(-x, y, -z); - } - if (typeOfSensor == Sensor.TYPE_GYROSCOPE) { - GodotLib.gyroscope(x, -y, z); - } - }); + switch (typeOfSensor) { + case Sensor.TYPE_ACCELEROMETER: { + float[] rotatedValues = getRotatedValues(event.values); + mRenderView.queueOnRenderThread(() -> { + GodotLib.accelerometer(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]); + }); + break; + } + case Sensor.TYPE_GRAVITY: { + float[] rotatedValues = getRotatedValues(event.values); + mRenderView.queueOnRenderThread(() -> { + GodotLib.gravity(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]); + }); + break; + } + case Sensor.TYPE_MAGNETIC_FIELD: { + float[] rotatedValues = getRotatedValues(event.values); + mRenderView.queueOnRenderThread(() -> { + GodotLib.magnetometer(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]); + }); + break; + } + case Sensor.TYPE_GYROSCOPE: { + float[] rotatedValues = getRotatedValues(event.values); + mRenderView.queueOnRenderThread(() -> { + GodotLib.gyroscope(rotatedValues[0], rotatedValues[1], rotatedValues[2]); + }); + break; + } } } |