skip space cells at 0

This commit is contained in:
jacob 2025-01-28 20:31:52 -06:00
parent 70e2d4a935
commit dbd16ccd67
2 changed files with 18 additions and 6 deletions

View File

@ -15,6 +15,9 @@ READONLY struct space _g_space_nil = { .valid = false };
* Space alloc
* ========================== */
/* NOTE:
* The number of buckets determines how often tiles will collide in the spatial hash.
* For example, at `num_buckets_sqrt` = 256 (65536 buckets), tiles <1, 1>, <1, 257>, and <257, 257> will collide. */
struct space *space_alloc(f32 cell_size, u32 num_buckets_sqrt)
{
struct arena arena = arena_alloc(GIGABYTE(64));
@ -68,6 +71,9 @@ INTERNAL i32 cell_coords_to_bucket_index(struct space *space, struct v2i32 cell_
{
i32 num_buckets_sqrt = space->num_buckets_sqrt;
/* Cell pos of 0 is not valid and will be converted to -1 */
ASSERT(cell_pos.x != 0 && cell_pos.y != 0);
i32 index_x = cell_pos.x;
i32 index_y = cell_pos.y;
/* Offset cell index by -1 since cell pos of 0 is invalid */
@ -291,13 +297,17 @@ void space_client_release(struct space_client *client)
void space_client_update_aabb(struct space_client *client, struct aabb new_aabb)
{
struct space *space = space_from_client(client);
f32 cell_size = space->cell_size;
struct aabb old_aabb = client->aabb;
struct v2i32 old_cell_p0 = world_to_cell_coords(cell_size, old_aabb.p0);
struct v2i32 old_cell_p1 = world_to_cell_coords(cell_size, old_aabb.p1);
struct v2i32 old_cell_p0 = V2I32(0, 0);
struct v2i32 old_cell_p1 = V2I32(0, 0);
if (client->first_node) {
struct aabb old_aabb = client->aabb;
old_cell_p0 = world_to_cell_coords(cell_size, old_aabb.p0);
old_cell_p1 = world_to_cell_coords(cell_size, old_aabb.p1);
}
struct v2i32 new_cell_p0 = world_to_cell_coords(cell_size, new_aabb.p0);
struct v2i32 new_cell_p1 = world_to_cell_coords(cell_size, new_aabb.p1);
@ -319,7 +329,7 @@ void space_client_update_aabb(struct space_client *client, struct aabb new_aabb)
/* Insert new nodes */
for (i32 y = new_cell_p0.y; y <= new_cell_p1.y; ++y) {
for (i32 x = new_cell_p0.x; x <= new_cell_p1.x; ++x) {
if (x < old_cell_p0.x || x > old_cell_p1.x || y < old_cell_p0.y || y > old_cell_p1.y) {
if (x != 0 && y != 0 && (x < old_cell_p0.x || x > old_cell_p1.x || y < old_cell_p0.y || y > old_cell_p1.y)) {
/* Cell is outside of old AABB */
space_cell_node_alloc(V2I32(x, y), client);
}
@ -396,6 +406,8 @@ struct space_client *space_iter_next(struct space_iter *iter)
if (nextx || nexty) {
cell_cur.x += nextx - (span * nexty);
cell_cur.y += nexty;
cell_cur.x += (cell_cur.x == 0);
cell_cur.y += (cell_cur.y == 0);
iter->cell_cur = cell_cur;
struct space_cell *cell = space_get_cell(space, cell_cur);
next_node = cell->first_node;

View File

@ -374,7 +374,7 @@ INTERNAL void queue_game_cmd(struct arena *arena, struct game_cmd_list *list, st
{
struct game_cmd_node *node = arena_push_zero(arena, struct game_cmd_node);
node->cmd = cmd;
if (list->first) {
if (list->last) {
list->last->next = node;
} else {
list->first = node;