From 096a5f8dbdb668e9a97156b208254b3e0946ef78 Mon Sep 17 00:00:00 2001 From: John S <138552829+Multi-Volt@users.noreply.github.com> Date: Thu, 19 Dec 2024 20:15:16 -0500 Subject: [PATCH] Fix the (0,0) edge case in the atan2s offset lookup table (#576) --- src/engine/math_util.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/engine/math_util.c b/src/engine/math_util.c index c85ac1567..349f5f3e0 100644 --- a/src/engine/math_util.c +++ b/src/engine/math_util.c @@ -849,8 +849,11 @@ inline s16 atan2s(f32 y, f32 x) { static const s16 offsets[] = {0x4000, 0x4000, 0xC000, 0xC000, 0x0000, 0x8000, 0x0000, 0x8000}; static const s8 signs[] = {-1, 1, 1, -1, 1, -1, -1, 1}; + // Adjust output for (0, 0) edge case + s16 zeroAdj = (x == 0.0f && y == 0.0f) * -0x4000; + // Ensure the result fits into 16 bits via an explicit cast on angle - return (offsets[idx] + (signs[idx] * (s16)angle)) & 0xFFFF; + return ((offsets[idx] + (signs[idx] * (s16)angle)) + zeroAdj) & 0xFFFF; } /**