Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Only rotate Android sensor values for sensors that need them rotated #55184

Merged
merged 1 commit into from
Dec 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 64 additions & 31 deletions platform/android/java/lib/src/org/godotengine/godot/Godot.java
Original file line number Diff line number Diff line change
Expand Up @@ -856,45 +856,78 @@ public void UiChangeListener() {
});
}

@Override
public void onSensorChanged(SensorEvent event) {
public float[] getRotatedValues(float values[]) {
madmiraal marked this conversation as resolved.
Show resolved Hide resolved
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 (mView == null) {
return;
}

final int typeOfSensor = event.sensor.getType();
if (mView != null) {
mView.queueEvent(() -> {
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);
mView.queueEvent(() -> {
GodotLib.accelerometer(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]);
});
break;
}
case Sensor.TYPE_GRAVITY: {
float[] rotatedValues = getRotatedValues(event.values);
mView.queueEvent(() -> {
GodotLib.gravity(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]);
});
break;
}
case Sensor.TYPE_MAGNETIC_FIELD: {
float[] rotatedValues = getRotatedValues(event.values);
mView.queueEvent(() -> {
GodotLib.magnetometer(-rotatedValues[0], -rotatedValues[1], -rotatedValues[2]);
});
break;
}
case Sensor.TYPE_GYROSCOPE: {
float[] rotatedValues = getRotatedValues(event.values);
mView.queueEvent(() -> {
GodotLib.gyroscope(rotatedValues[0], rotatedValues[1], rotatedValues[2]);
});
break;
}
}
}

Expand Down