enforce limits
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;
@@ -12,8 +15,6 @@ import org.openbot.utils.Enums.DriveMode;
import org.openbot.vehicle.Control;
public class GameController {
- private float left = 0;
- private float right = 0;
private DriveMode driveMode;
public GameController(DriveMode driveMode) {
@@ -158,7 +159,7 @@ public class GameController {
if (right >= 0) right -= steeringOffset;
else right += steeringOffset;
- return new Control(left, right);
+ return new Control(enforceLimits(left), enforceLimits(right));
}
public Control convertJoystickToControl(float xAxis, float yAxis) {
@@ -170,7 +171,7 @@ public class GameController {
if (right >= 0) right -= xAxis;
else right += xAxis;
- return new Control(left, right);
+ return new Control(enforceLimits(left), enforceLimits(right));
}
public static Pair<Float, Float> processJoystickInputLeft(MotionEvent event, int historyPos) {
@@ -202,4 +203,8 @@ public class GameController {
return new Pair<>(x, y);
}
+
+ private float enforceLimits(float control) {
+ return max(-1.f, min(control, 1.f));
+ }
}
