From f759e647d3bc6276e0392b3d7951ad864694bd1d Mon Sep 17 00:00:00 2001 From: Matteo Hausner Date: Tue, 31 Dec 2024 16:19:17 +0100 Subject: [PATCH] Take into account rounding errors during conversion from float to int --- .../action/AxisToRelativeAxisAction.java | 18 ++++++++-------- .../input/action/ToCursorAction.java | 21 +++++++++---------- .../input/action/ToScrollAction.java | 12 +++++------ 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToRelativeAxisAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToRelativeAxisAction.java index 53e9a1a2..4443af0a 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToRelativeAxisAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/AxisToRelativeAxisAction.java @@ -52,21 +52,21 @@ public void doAction(final Input input, final int component, final Float value) var d = Input.normalize(Math.signum(value) * (float) Math.pow((absValue - deadZone) * 100f, exponent), -inMax, inMax, -maxRelativeSpeed, maxRelativeSpeed) * input.getRateMultiplier(); - d += remainingD; if (Math.abs(d) < input.getPlanckLength()) { remainingD = d; - } else { - remainingD = 0f; + return; + } - final var runMode = input.getRunMode(); - final var oldValue = Input.normalize(input.getAxes().get(virtualAxis), runMode.getMinAxisValue(), - runMode.getMaxAxisValue(), -1f, 1f); + final var runMode = input.getRunMode(); + final var oldValue = Input.normalize(input.getAxes().get(virtualAxis), runMode.getMinAxisValue(), + runMode.getMaxAxisValue(), -1f, 1f); - final var newValue = Math.min(Math.max(oldValue + (invert ? -d : d), minValue), maxValue); - input.setAxis(virtualAxis, newValue, hapticFeedback, detentValue); - } + final var newValue = Math.min(Math.max(oldValue + (invert ? -d : d), minValue), maxValue); + input.setAxis(virtualAxis, newValue, hapticFeedback, detentValue); + + remainingD = 0f; } public Float getDetentValue() { diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToCursorAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToCursorAction.java index a6bb8678..80a15156 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToCursorAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToCursorAction.java @@ -54,22 +54,21 @@ public String getDescription(final Input input) { void moveCursor(final Input input, float d) { d = invert ? -d : d; - d += remainingD; - if (d >= -1f && d <= 1f) { + final var roundedD = Math.round(d); + if (roundedD == 0) { remainingD = d; - } else { - remainingD = 0f; - - final var intD = Math.round(d); + return; + } - if (axis == MouseAxis.X) { - input.setCursorDeltaX(input.getCursorDeltaX() + intD); - } else { - input.setCursorDeltaY(input.getCursorDeltaY() + intD); - } + if (axis == MouseAxis.X) { + input.setCursorDeltaX(input.getCursorDeltaX() + roundedD); + } else { + input.setCursorDeltaY(input.getCursorDeltaY() + roundedD); } + + remainingD = d - roundedD; } public void setAxis(final MouseAxis axis) { diff --git a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToScrollAction.java b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToScrollAction.java index bef82390..0d1d2388 100644 --- a/src/main/java/de/bwravencl/controllerbuddy/input/action/ToScrollAction.java +++ b/src/main/java/de/bwravencl/controllerbuddy/input/action/ToScrollAction.java @@ -48,16 +48,16 @@ public String getDescription(final Input input) { void scroll(final Input input, float d) { d = invert ? -d : d; - d += remainingD; - if (d >= -1f && d <= 1f) { + final var roundedD = Math.round(d); + if (roundedD == 0) { remainingD = d; - } else { - remainingD = 0f; - - input.setScrollClicks(Math.round(d)); + return; } + + input.setScrollClicks(roundedD); + remainingD = d - roundedD; } public void setClicks(final int clicks) {