From 894c2dfa26486acdc7fdeb6e36777ec05a763d54 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 28 Jan 2025 09:11:23 -0600 Subject: [PATCH] release space client when entity releases. fix aabb test --- src/collider.c | 19 ++++++++++--------- src/game.c | 20 ++++++++++++++++---- src/phys.c | 1 + src/space.c | 7 ++++--- 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/collider.c b/src/collider.c index b4084828..407c8578 100644 --- a/src/collider.c +++ b/src/collider.c @@ -118,15 +118,16 @@ struct aabb collider_aabb_from_combined_aabb(struct aabb b0, struct aabb b1) b32 collider_test_aabb(struct aabb box0, struct aabb box1) { - b32 res = (box0.p0.x >= box1.p0.x && box0.p0.x <= box1.p1.x && box0.p0.y >= box1.p0.y && box0.p0.y <= box1.p1.y) || /* Test box0 top left inside box1 */ - (box0.p1.x >= box1.p0.x && box0.p1.x <= box1.p1.x && box0.p0.y >= box1.p0.y && box0.p0.y <= box1.p1.y) || /* Test box0 top right inside box1 */ - (box0.p1.x >= box1.p0.x && box0.p1.x <= box1.p1.x && box0.p1.y >= box1.p0.y && box0.p1.y <= box1.p1.y) || /* Test box0 bottom right inside box1 */ - (box0.p0.x >= box1.p0.x && box0.p0.x <= box1.p1.x && box0.p1.y >= box1.p0.y && box0.p1.y <= box1.p1.y) || /* Test box0 bottom left inside box1 */ - (box1.p0.x >= box0.p0.x && box1.p0.x <= box0.p1.x && box1.p0.y >= box0.p0.y && box1.p0.y <= box0.p1.y) || /* Test box1 top left inside box0 */ - (box1.p1.x >= box0.p0.x && box1.p1.x <= box0.p1.x && box1.p0.y >= box0.p0.y && box1.p0.y <= box0.p1.y) || /* Test box1 top right inside box0 */ - (box1.p1.x >= box0.p0.x && box1.p1.x <= box0.p1.x && box1.p1.y >= box0.p0.y && box1.p1.y <= box0.p1.y) || /* Test box1 bottom right inside box0 */ - (box1.p0.x >= box0.p0.x && box1.p0.x <= box0.p1.x && box1.p1.y >= box0.p0.y && box1.p1.y <= box0.p1.y); /* Test box1 bottom left inside box0 */ - return res; + f32 b0_x0 = box0.p0.x; + f32 b0_x1 = box0.p1.x; + f32 b1_x0 = box1.p0.x; + f32 b1_x1 = box1.p1.x; + f32 b0_y0 = box0.p0.y; + f32 b0_y1 = box0.p1.y; + f32 b1_y0 = box1.p0.y; + f32 b1_y1 = box1.p1.y; + return ((b0_x0 >= b1_x0 && b0_x0 <= b1_x1) || (b0_x1 >= b1_x0 && b0_x1 <= b1_x1) || (b1_x0 >= b0_x0 && b1_x0 <= b0_x1) || (b1_x1 >= b0_x0 && b1_x1 <= b0_x1)) && + ((b0_y0 >= b1_y0 && b0_y0 <= b1_y1) || (b0_y1 >= b1_y0 && b0_y1 <= b1_y1) || (b1_y0 >= b0_y0 && b1_y0 <= b0_y1) || (b1_y1 >= b0_y0 && b1_y1 <= b0_y1)); } /* ========================== * diff --git a/src/game.c b/src/game.c index 0087dcc2..8b5b5fd0 100644 --- a/src/game.c +++ b/src/game.c @@ -338,9 +338,7 @@ INTERNAL void release_entities_with_prop(enum entity_prop prop) { struct temp_arena scratch = scratch_begin_no_conflict(); struct entity_store *store = G.tick.entity_store; - - /* TODO: Breadth first iteration to only release parent entities (since - * child entities will be released along with parent anyway) */ + struct space *space = G.space; struct entity **ents_to_release = arena_dry_push(scratch.arena, struct entity *); u64 ents_to_release_count = 0; @@ -354,10 +352,24 @@ INTERNAL void release_entities_with_prop(enum entity_prop prop) } } + /* Release references */ + for (u64 i = 0; i < ents_to_release_count; ++i) { + struct entity *ent = ents_to_release[i]; + /* Release space client */ + { + struct space_client *space_client = space_client_from_handle(space, ent->space_handle); + if (space_client->valid) { + space_client_release(space_client); + } + } + } + + /* Release from store */ + /* TODO: Breadth first iteration to only release parent entities (since + * child entities will be released along with parent anyway) */ for (u64 i = 0; i < ents_to_release_count; ++i) { struct entity *ent = ents_to_release[i]; if (ent->valid) { - /* Release */ entity_release(store, ent); } } diff --git a/src/phys.c b/src/phys.c index 04a7208d..aa294a57 100644 --- a/src/phys.c +++ b/src/phys.c @@ -361,6 +361,7 @@ void phys_prepare_contacts(struct phys_ctx *ctx, u64 phys_iteration) } } else { /* Mark constraint for removal */ + constraint_ent->contact_constraint_data.num_points = 0; entity_disable_prop(constraint_ent, ENTITY_PROP_ACTIVE); entity_enable_prop(constraint_ent, ENTITY_PROP_RELEASE_THIS_TICK); /* Remove from lookup */ diff --git a/src/space.c b/src/space.c index 23471cd5..5434ca36 100644 --- a/src/space.c +++ b/src/space.c @@ -363,6 +363,7 @@ struct space_client *space_iter_next(struct space_iter *iter) struct v2i32 cell_start = iter->cell_start; struct v2i32 cell_end = iter->cell_end; struct v2i32 cell_cur = iter->cell_cur; + i32 span = cell_end.x - cell_start.x; struct space_cell_node *next_node = NULL; if (cell_cur.x >= cell_start.x && cell_cur.x <= cell_end.x && cell_cur.y >= cell_start.y && cell_cur.y <= cell_end.y) { @@ -391,10 +392,10 @@ struct space_client *space_iter_next(struct space_iter *iter) } else { /* Reached end of cell, find next cell */ b32 nextx = (cell_cur.x + 1) <= cell_end.x; - b32 nexty = (cell_cur.y + 1) <= cell_end.y; + b32 nexty = (nextx == 0) && ((cell_cur.y + 1) <= cell_end.y); if (nextx || nexty) { - cell_cur.x += 1 * nextx; - cell_cur.y += 1 * nexty; + cell_cur.x += nextx - (span * nexty); + cell_cur.y += nexty; iter->cell_cur = cell_cur; struct space_cell *cell = space_get_cell(space, cell_cur); next_node = cell->first_node;