From 7229daf49c35060898f7e6d791500c407ba7f4be Mon Sep 17 00:00:00 2001 From: jacob Date: Sun, 8 Jun 2025 14:10:27 -0500 Subject: [PATCH] draw collider fast path when radius=0 --- src/draw.c | 36 +++++++++++++++++++++++++++--------- src/draw.h | 2 +- src/user.c | 8 +++----- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/draw.c b/src/draw.c index 777afbcc..59432a44 100644 --- a/src/draw.c +++ b/src/draw.c @@ -239,21 +239,39 @@ void draw_arrow_ray(struct gpu_handle cmd_list, struct v2 pos, struct v2 rel, f3 draw_arrow_line(cmd_list, pos, end, thickness, arrowhead_height, color); } -void draw_collider_line(struct gpu_handle cmd_list, struct xform draw_xf, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail) +void draw_collider_line(struct gpu_handle cmd_list, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail) { + /* FIXME: Graphics bug when lots of colliders are drawn */ + DEBUGBREAKABLE; struct arena_temp scratch = scratch_begin_no_conflict(); - struct v2 *points = arena_push_array_no_zero(scratch.arena, struct v2, detail); - for (u32 i = 0; i < detail; ++i) { - f32 angle = ((f32)i / (f32)detail) * (2 * PI); - struct v2 dir = V2(math_cos(angle), math_sin(angle)); - struct v2 p = collider_get_support_point(&shape, shape_xf, dir).p; - p = xform_mul_v2(draw_xf, p); - points[i] = p; + struct v2_array poly = ZI; + + if (shape.radius == 0) { + poly.count = shape.count; + poly.points = arena_push_array_no_zero(scratch.arena, struct v2, shape.count); + for (u32 i = 0; i < shape.count; ++i) { + struct v2 p = xform_mul_v2(shape_xf, shape.points[i]); + poly.points[i] = p; + } + } else { + poly.count = detail; + poly.points = arena_push_array_no_zero(scratch.arena, struct v2, detail); + for (u32 i = 0; i < detail; ++i) { + f32 angle = ((f32)i / (f32)detail) * (2 * PI); + struct v2 dir = V2(math_cos(angle), math_sin(angle)); + struct v2 p = collider_get_support_point(&shape, shape_xf, dir).p; + poly.points[i] = p; + } } - struct v2_array poly = { .points = points, .count = detail }; + DEBUGBREAKABLE; +#if 1 draw_poly_line(cmd_list, poly, true, thickness, color); +#else + draw_poly(cmd_list, poly, color); + (UNUSED)thickness; +#endif scratch_end(scratch); } diff --git a/src/draw.h b/src/draw.h index 65d46988..f1e07477 100644 --- a/src/draw.h +++ b/src/draw.h @@ -69,7 +69,7 @@ void draw_arrow_line(struct gpu_handle cmd_list, struct v2 start, struct v2 end, void draw_arrow_ray(struct gpu_handle cmd_list, struct v2 pos, struct v2 rel, f32 thickness, f32 arrowhead_height, u32 color); -void draw_collider_line(struct gpu_handle cmd_list, struct xform draw_xf, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail); +void draw_collider_line(struct gpu_handle cmd_list, struct collider_shape shape, struct xform shape_xf, f32 thickness, u32 color, u32 detail); /* ========================== * * Grid diff --git a/src/user.c b/src/user.c index cf44c0f8..937a2384 100644 --- a/src/user.c +++ b/src/user.c @@ -1226,7 +1226,7 @@ INTERNAL void user_update(void) b32 skip_debug_draw_transform = sim_ent_has_prop(ent, SEPROP_CAMERA); skip_debug_draw_transform = true; - struct xform sprite_xform = xf; + struct xform sprite_xform = xform_mul(xf, ent->sprite_local_xform); /* Draw tracer */ if (sim_ent_has_prop(ent, SEPROP_TRACER)) { @@ -1269,9 +1269,6 @@ INTERNAL void user_update(void) /* Draw sprite */ //if ((false)) { if (!sprite_tag_is_nil(sprite)) { - /* Calculate sprite xform */ - sprite_xform = xform_mul(xf, ent->sprite_local_xform); - /* Async load */ struct sprite_sheet *sheet = sprite_sheet_from_tag_async(sprite_frame_scope, sprite); struct sprite_texture *texture = sprite_texture_from_tag_async(sprite_frame_scope, sprite); @@ -1422,7 +1419,8 @@ INTERNAL void user_update(void) { /* Draw collider using support points */ u32 detail = 32; - draw_collider_line(G.user_gpu_cmd_list, G.world_to_user_xf, collider, xf, thickness, color, detail); + struct xform collider_draw_xf = xform_mul(G.world_to_user_xf, xf); + draw_collider_line(G.user_gpu_cmd_list, collider, collider_draw_xf, thickness, color, detail); } { /* Draw collider shape points */