diff --git a/android/app/src/main/java/org/openbot/env/GameController.java b/android/app/src/main/java/org/openbot/env/GameController.java index 06e4d47ef..432f6e1c0 100644 --- a/android/app/src/main/java/org/openbot/env/GameController.java +++ b/android/app/src/main/java/org/openbot/env/GameController.java @@ -2,9 +2,6 @@ package org.openbot.env; -import static java.lang.Math.max; -import static java.lang.Math.min; - import android.util.Pair; import android.view.InputDevice; import android.view.KeyEvent; @@ -159,7 +156,7 @@ public Control convertGameToControl(float leftTrigger, float rightTrigger, float if (right >= 0) right -= steeringOffset; else right += steeringOffset; - return new Control(enforceLimits(left), enforceLimits(right)); + return new Control(left, right); } public Control convertJoystickToControl(float xAxis, float yAxis) { @@ -171,7 +168,7 @@ public Control convertJoystickToControl(float xAxis, float yAxis) { if (right >= 0) right -= xAxis; else right += xAxis; - return new Control(enforceLimits(left), enforceLimits(right)); + return new Control(left, right); } public static Pair processJoystickInputLeft(MotionEvent event, int historyPos) { @@ -203,8 +200,4 @@ public static Pair processJoystickInputRight(MotionEvent event, in return new Pair<>(x, y); } - - private float enforceLimits(float control) { - return max(-1.f, min(control, 1.f)); - } } diff --git a/android/app/src/test/java/org/openbot/env/GameControllerTest.java b/android/app/src/test/java/org/openbot/env/GameControllerTest.java new file mode 100644 index 000000000..1a2c3fb06 --- /dev/null +++ b/android/app/src/test/java/org/openbot/env/GameControllerTest.java @@ -0,0 +1,50 @@ +package org.openbot.env; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.openbot.utils.Enums; +import org.openbot.vehicle.Control; + +public class GameControllerTest { + @Test + public void convertDualToControl_test() { + GameController gameController = new GameController(Enums.DriveMode.DUAL); + assertEquals(Enums.DriveMode.DUAL, gameController.getDriveMode()); + Control control = gameController.convertDualToControl(0.5f, -0.5f); + assertEquals(control.getLeft(), -0.5f, 0.0f); + assertEquals(control.getRight(), 0.5f, 0.0f); + } + + @Test + public void convertGameToControl_test() { + GameController gameController = new GameController(Enums.DriveMode.GAME); + assertEquals(Enums.DriveMode.GAME, gameController.getDriveMode()); + Control control; + control = gameController.convertGameToControl(0.0f, 0.5f, 1.0f); + assertEquals(control.getLeft(), 1.0f, 0.0f); + assertEquals(control.getRight(), -0.5f, 0.0f); + + control = gameController.convertGameToControl(0.0f, 0.5f, -1.0f); + assertEquals(control.getLeft(), -0.5f, 0.0f); + assertEquals(control.getRight(), 1.0f, 0.0f); + + control = gameController.convertGameToControl(0.0f, 0.5f, 0.0f); + assertEquals(control.getLeft(), 0.5f, 0.0f); + assertEquals(control.getRight(), 0.5f, 0.0f); + } + + @Test + public void convertJoystickToControl_test() { + GameController gameController = new GameController(Enums.DriveMode.JOYSTICK); + assertEquals(Enums.DriveMode.JOYSTICK, gameController.getDriveMode()); + Control control; + control = gameController.convertJoystickToControl(0.5f, -0.5f); + assertEquals(control.getLeft(), 1.0f, 0.0f); + assertEquals(control.getRight(), 0.0f, 0.0f); + + control = gameController.convertJoystickToControl(-0.5f, -0.5f); + assertEquals(control.getLeft(), 0.0f, 0.0f); + assertEquals(control.getRight(), 1.0f, 0.0f); + } +}