From b9e63dea26468403cb0ffe5293cf4f6447d8919f Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 28 Jan 2020 15:17:38 +0400 Subject: [PATCH] Fixed angle wrapping math. When rotation was < 0, we were subtracting it from 360, which was producing a number larger than 360. We should have been adding it to 360, not subtracting. Moved angle wrapping math to a separate function. --- core/src/com/me/asteroids/Utils.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/core/src/com/me/asteroids/Utils.java b/core/src/com/me/asteroids/Utils.java index a74f9a1..bbc6576 100644 --- a/core/src/com/me/asteroids/Utils.java +++ b/core/src/com/me/asteroids/Utils.java @@ -10,13 +10,15 @@ public final class Utils { private static final Vector2 tmp = new Vector2(); public static float rotate(float rotation, float degrees) { - rotation += degrees; - if (rotation < 0) { - rotation = 360 - rotation; - } else if (rotation > 360) { - rotation -= 360; - } - return rotation; + return wrapAngle(rotation + degrees); + } + + public static float wrapAngle(float degrees) { + if (degrees < 0) + return degrees + 360; + else if (degrees > 360) + return degrees - 360; + return degrees; } public static Vector2 setUnitVectorAngle(Vector2 vector, float degrees) {