From 1e8b6973c16646f7c9d325942f247374d12a30ee Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 26 Aug 2024 19:21:14 -0500 Subject: [PATCH] replace atan2 approximation with one that seems more stable --- src/game.c | 5 ++--- src/math.h | 35 ++++++++++++----------------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/game.c b/src/game.c index ac8431a3..2bec7231 100644 --- a/src/game.c +++ b/src/game.c @@ -124,7 +124,7 @@ INTERNAL void spawn_test_entities(void) struct v2 pos = V2(-1, -1); struct v2 size = V2(1, 1); f32 r = 0; - f32 skew = PI / 4; + f32 skew = 0; struct entity *e = entity_alloc(root); @@ -145,7 +145,7 @@ INTERNAL void spawn_test_entities(void) e->mass_unscaled = 70; e->ground_friction = 1000; - entity_enable_prop(e, ENTITY_PROP_TEST); + //entity_enable_prop(e, ENTITY_PROP_TEST); player_ent = e; } @@ -918,7 +918,6 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) ent->camera_applied_lerp_continuity_gen_plus_one = ent->camera_lerp_continuity_gen + 1; entity_set_xform(ent, xf); } - } /* ========================== * diff --git a/src/math.h b/src/math.h index d1d8b149..716a7adb 100644 --- a/src/math.h +++ b/src/math.h @@ -434,31 +434,20 @@ INLINE f32 math_cos(f32 x) return math_sin(x + (PI / 2.0f)); } -/* https://mazzo.li/posts/vectorized-atan2.html */ +/* http://math.stackexchange.com/questions/1098487/atan2-faster-approximation */ +/* FIXME: Verify quadrants */ INLINE f32 math_atan2(f32 y, f32 x) { - const f32 a1 = 0.99997726f; - const f32 a3 = -0.33262347f; - const f32 a5 = 0.19354346f; - const f32 a7 = -0.11643287f; - const f32 a9 = 0.05265332f; - const f32 a11 = -0.01172120f; - - /* Ensure input is in [-1, +1] */ - b32 swap = math_fabs(x) < math_fabs(y); - f32 s = (swap ? x : y) / (swap ? y : x); - - /* Approximate atan */ - f32 s_sq = s * s; - f32 res = s * (a1 + s_sq * (a3 + s_sq * (a5 + s_sq * (a7 + s_sq * (a9 + s_sq * a11))))); - res = swap ? (s >= 0.0f ? (PI / 2.f) : -(PI / 2.f)) - res : res; - - /* Adjust quadrants */ - if (x < 0.0f && y >= 0.0f) { res = PI + res; } /* 2nd quadrant */ - else if (x <= 0.0f && y < 0.0f) { res = -PI + res; } /* 3rd quadrant */ - else if (x == 0.0f && y == 0.0f) { res = 0; } /* NaN -> 0 */ - - return res; + f32 x_abs = math_fabs(x); + f32 y_abs = math_fabs(y); + b32 swap = y_abs > x_abs; + f32 a = swap ? x_abs / y_abs : y_abs / x_abs; + f32 s = a * a; + f32 r = ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a; + if (swap) { r = (PI / 2) - r; } + if (x < 0) { r = PI - r; } + if (y < 0) { r = -r; } + return r; } INLINE f32 math_asin(f32 x)