From 40a20439e529cdb7b0dd4658db1596efce7c43ee Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 28 May 2025 03:43:34 -0500 Subject: [PATCH] fix contact direction skip not updating --- src/phys.c | 14 ++++++++------ src/phys.h | 1 + src/user.c | 4 +--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/phys.c b/src/phys.c index 020ec5cb..35f984c0 100644 --- a/src/phys.c +++ b/src/phys.c @@ -174,12 +174,14 @@ void phys_create_and_update_contacts(struct phys_step_ctx *ctx, f32 elapsed_dt, struct v2 dir0 = e0->collision_dir; struct v2 dir1 = e1->collision_dir; f32 threshold = 0.5; - if (!v2_is_zero(dir0) && !constraint->skip_solve) { - constraint->skip_solve = v2_dot(dir0, normal) <= threshold; + b32 is_wrong_dir = false; + if (!v2_is_zero(dir0)) { + is_wrong_dir = v2_dot(dir0, normal) <= threshold; } - if (!v2_is_zero(dir1) && !constraint->skip_solve) { - constraint->skip_solve = v2_dot(dir1, v2_neg(normal)) <= threshold; + if (!v2_is_zero(dir1) && !is_wrong_dir) { + is_wrong_dir = v2_dot(dir1, v2_neg(normal)) <= threshold; } + constraint->wrong_dir = is_wrong_dir; } /* Run collision callback */ @@ -396,7 +398,7 @@ void phys_warm_start_contacts(struct phys_step_ctx *ctx) struct sim_ent *e0 = sim_ent_from_id(ss, constraint->e0); struct sim_ent *e1 = sim_ent_from_id(ss, constraint->e1); - if (num_points > 0 && sim_ent_is_valid_and_active(e0) && sim_ent_is_valid_and_active(e1) && !constraint->skip_solve) { + if (num_points > 0 && sim_ent_is_valid_and_active(e0) && sim_ent_is_valid_and_active(e1) && !constraint->skip_solve && !constraint->wrong_dir) { f32 inv_m0 = constraint->inv_m0; f32 inv_m1 = constraint->inv_m1; f32 inv_i0 = constraint->inv_i0; @@ -453,7 +455,7 @@ void phys_solve_contacts(struct phys_step_ctx *ctx, f32 dt, b32 apply_bias) f32 w1 = e1->angular_velocity; u32 num_points = constraint->num_points; - if (num_points > 0 && sim_ent_is_valid_and_active(e0) && sim_ent_is_valid_and_active(e1) && !constraint->skip_solve) { + if (num_points > 0 && sim_ent_is_valid_and_active(e0) && sim_ent_is_valid_and_active(e1) && !constraint->skip_solve && !constraint->wrong_dir) { struct xform e0_xf = sim_ent_get_xform(e0); struct xform e1_xf = sim_ent_get_xform(e1); diff --git a/src/phys.h b/src/phys.h index e11f602a..a5bb5d05 100644 --- a/src/phys.h +++ b/src/phys.h @@ -61,6 +61,7 @@ struct phys_contact_point { struct phys_contact_constraint { u64 last_phys_iteration; /* To avoid checking collisions for the same constraint twice in one tick */ b32 skip_solve; + b32 wrong_dir; struct sim_ent_id e0; struct sim_ent_id e1; f32 inv_m0; diff --git a/src/user.c b/src/user.c index 86c87ffd..691a61a8 100644 --- a/src/user.c +++ b/src/user.c @@ -1422,13 +1422,12 @@ INTERNAL void user_update(void) (UNUSED)e0; (UNUSED)e1; -#if 1 #if DEVELOPER /* Draw contact points */ { f32 radius = 5; for (u32 i = 0; i < data->num_points; ++i) { - u32 color = data->skip_solve ? ALPHA_F(COLOR_YELLOW, 0.3) : RGBA_F(0.8, 0.2, 0.2, 1); + u32 color = (data->skip_solve || data->wrong_dir) ? ALPHA_F(COLOR_YELLOW, 0.3) : RGBA_F(0.8, 0.2, 0.2, 1); struct phys_contact_point point = data->points[i]; struct v2 dbg_pt = point.dbg_pt; @@ -1672,7 +1671,6 @@ INTERNAL void user_update(void) } #endif } -#endif #endif /* Draw hierarchy */