replace atan2 approximation with one that seems more stable

This commit is contained in:
jacob 2024-08-26 19:21:14 -05:00
parent bdcc6a8c81
commit 1e8b6973c1
2 changed files with 14 additions and 26 deletions

View File

@ -124,7 +124,7 @@ INTERNAL void spawn_test_entities(void)
struct v2 pos = V2(-1, -1); struct v2 pos = V2(-1, -1);
struct v2 size = V2(1, 1); struct v2 size = V2(1, 1);
f32 r = 0; f32 r = 0;
f32 skew = PI / 4; f32 skew = 0;
struct entity *e = entity_alloc(root); struct entity *e = entity_alloc(root);
@ -145,7 +145,7 @@ INTERNAL void spawn_test_entities(void)
e->mass_unscaled = 70; e->mass_unscaled = 70;
e->ground_friction = 1000; e->ground_friction = 1000;
entity_enable_prop(e, ENTITY_PROP_TEST); //entity_enable_prop(e, ENTITY_PROP_TEST);
player_ent = e; 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; ent->camera_applied_lerp_continuity_gen_plus_one = ent->camera_lerp_continuity_gen + 1;
entity_set_xform(ent, xf); entity_set_xform(ent, xf);
} }
} }
/* ========================== * /* ========================== *

View File

@ -434,31 +434,20 @@ INLINE f32 math_cos(f32 x)
return math_sin(x + (PI / 2.0f)); 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) INLINE f32 math_atan2(f32 y, f32 x)
{ {
const f32 a1 = 0.99997726f; f32 x_abs = math_fabs(x);
const f32 a3 = -0.33262347f; f32 y_abs = math_fabs(y);
const f32 a5 = 0.19354346f; b32 swap = y_abs > x_abs;
const f32 a7 = -0.11643287f; f32 a = swap ? x_abs / y_abs : y_abs / x_abs;
const f32 a9 = 0.05265332f; f32 s = a * a;
const f32 a11 = -0.01172120f; f32 r = ((-0.0464964749 * s + 0.15931422) * s - 0.327622764) * s * a + a;
if (swap) { r = (PI / 2) - r; }
/* Ensure input is in [-1, +1] */ if (x < 0) { r = PI - r; }
b32 swap = math_fabs(x) < math_fabs(y); if (y < 0) { r = -r; }
f32 s = (swap ? x : y) / (swap ? y : x); return r;
/* 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;
} }
INLINE f32 math_asin(f32 x) INLINE f32 math_asin(f32 x)