From 69a8288d5d650d4b982da68c52f92c5a66a95bf1 Mon Sep 17 00:00:00 2001 From: Jack Zhou Date: Sun, 3 Jun 2018 17:54:21 -0400 Subject: [PATCH] Apply correct diagonal movement speeds Previously, diagonal movements were not reduced in the two axes, resulting in movement that was too quick. This commit divides diagonal movements by sqrt(2) / 2 to correct the movement speed. The net result is that diagonal movement should feel smoother and speed more as expected. --- src/MouseWrapper.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/MouseWrapper.cpp b/src/MouseWrapper.cpp index cca78610..604d897c 100644 --- a/src/MouseWrapper.cpp +++ b/src/MouseWrapper.cpp @@ -112,19 +112,34 @@ uint8_t MouseWrapper_::acceleration(uint8_t cycles) { return i; } +static int8_t roundAwayFromZero(float value) { + if (value < 0.0f) { + return static_cast(value - 0.5f); + } + + return static_cast(value + 0.5f); +} void MouseWrapper_::move(int8_t x, int8_t y) { int16_t moveX = 0; int16_t moveY = 0; static int8_t remainderX = 0; static int8_t remainderY = 0; + static const float HALF_SQRT_2 = 0.7071f; + + boolean isDiagonal = (x != 0 && y != 0); + if (x != 0) { + if (isDiagonal) x = roundAwayFromZero(HALF_SQRT_2 * x); moveX = remainderX + (x * acceleration(accelStep)); + if (moveX > (int16_t)speedLimit) moveX = speedLimit; else if (moveX < -(int16_t)speedLimit) moveX = -speedLimit; } if (y != 0) { + if (isDiagonal) y = roundAwayFromZero(HALF_SQRT_2 * y); moveY = remainderY + (y * acceleration(accelStep)); + if (moveY > (int16_t)speedLimit) moveY = speedLimit; else if (moveY < -(int16_t)speedLimit) moveY = -speedLimit; }