diff options
| author | iamcheeseman <[hidden email]> | 2026-01-14 15:20:56 -0500 |
|---|---|---|
| committer | iamcheeseman <[hidden email]> | 2026-01-14 15:20:56 -0500 |
| commit | 1b8553bf96017795dcf081b78371c3b2a8d5ecc5 (patch) | |
| tree | a8ea1e994755b9b461f37b621106dd59daf25329 /src/phys | |
| parent | 8d824c95f1b64bec6d7e298c7c7d925c4ba3422a (diff) | |
prefix private functions with underscore
Diffstat (limited to 'src/phys')
| -rw-r--r-- | src/phys/world.odin | 75 |
1 files changed, 52 insertions, 23 deletions
diff --git a/src/phys/world.odin b/src/phys/world.odin index cf66229..5bbfcbd 100644 --- a/src/phys/world.odin +++ b/src/phys/world.odin @@ -27,31 +27,31 @@ destroy_world :: proc() { } @(private = "file") -hash_bin :: proc(x: i32, y: i32) -> u32 { +_hash_bin :: proc(x: i32, y: i32) -> u32 { return transmute(u32)((x * 73856093) ~ (y * 19349663)) } @(private = "file") -world_to_bin :: proc(point: Vec2) -> (i32, i32) { +_world_to_bin :: proc(point: Vec2) -> (i32, i32) { return i32( math.floor(point.x / BIN_SIZE), ), i32(math.floor(point.y / BIN_SIZE)) } @(private = "file") -get_surrounding_bins :: proc( +_get_surrounding_bins :: proc( pos: Vec2, allocator := context.temp_allocator, ) -> []^[dynamic]Body_Handle { neighbors := make([]^[dynamic]Body_Handle, 9, allocator) - center_x, center_y := world_to_bin(pos) + center_x, center_y := _world_to_bin(pos) idx := 0 for offset_x in -1..=1 { for offset_y in -1..=1 { - bin_idx := hash_bin(center_x + i32(offset_x), center_y + i32(offset_y)) + bin_idx := _hash_bin(center_x + i32(offset_x), center_y + i32(offset_y)) bin := &world.bins[bin_idx % BIN_COUNT] neighbors[idx] = bin idx += 1 @@ -62,22 +62,22 @@ get_surrounding_bins :: proc( } @(private = "file") -find_bin :: proc(b: Body) -> ^[dynamic]Body_Handle { - bin_x, bin_y := world_to_bin(b.pos + b.rect.start) - return &world.bins[hash_bin(bin_x, bin_y) % BIN_COUNT] +_find_bin :: proc(b: Body) -> ^[dynamic]Body_Handle { + bin_x, bin_y := _world_to_bin(b.pos + b.rect.start) + return &world.bins[_hash_bin(bin_x, bin_y) % BIN_COUNT] } @(private = "file") -add_to_bins :: proc(b: ^Body) { - bin := find_bin(b^) +_add_to_bins :: proc(b: ^Body) { + bin := _find_bin(b^) idx := i32(len(bin)) append(bin, b.handle) b.bin_idx = idx } @(private = "file") -remove_from_bins :: proc(b: Body) { - bin := find_bin(b) +_remove_from_bins :: proc(b: Body) { + bin := _find_bin(b) assert(len(bin) > 0) assert(bin[b.bin_idx] == b.handle) @@ -115,7 +115,7 @@ add_body :: proc(b: Body) -> (Body_Handle, ^Body) { body := &world.bodies[world.handles[handle]] body.handle = handle - add_to_bins(body) + _add_to_bins(body) return handle, body } @@ -123,7 +123,7 @@ add_body :: proc(b: Body) -> (Body_Handle, ^Body) { remove_body :: proc(h: Body_Handle) { b := get_body(h) - remove_from_bins(b^) + _remove_from_bins(b^) last := pop(&world.bodies) if last.handle != h { @@ -134,6 +134,39 @@ remove_body :: proc(h: Body_Handle) { append(&world.unused_handles, h) } +get_collisions :: proc( + h: Body_Handle, + allocator := context.allocator +) -> []Body_Handle { + bodies := make([dynamic]Body_Handle, allocator) + + b := get_body(h) + + rect := b.rect + rect.start += b.pos + + bin_list := _get_surrounding_bins(rect.start) + + for bin in bin_list { + for c_h in bin { + if c_h == h { + continue + } + + c := get_body(c_h) + + c_rect := c.rect + c_rect.start += c.pos + + if aabb(rect, c_rect) { + append(&bodies, c_h) + } + } + } + + return bodies[:] +} + update_body :: proc(h: Body_Handle) { dt := rl.GetFrameTime() @@ -147,15 +180,11 @@ update_body :: proc(h: Body_Handle) { res_rect := b.rect res_rect.start += res_pos - bin_list := get_surrounding_bins(res_rect.start) + bin_list := _get_surrounding_bins(res_rect.start) b.collisions = {} - total_len := 0 - for bin in bin_list { - total_len += len(bin) - for c_h in bin { if c_h == h { continue @@ -203,13 +232,13 @@ update_body :: proc(h: Body_Handle) { set_body_position :: proc(h: Body_Handle, new_pos: Vec2) { b := get_body(h) - prev_bin := hash_bin(world_to_bin(b.pos + b.rect.start)) - res_bin := hash_bin(world_to_bin(new_pos + b.rect.start)) + prev_bin := _hash_bin(_world_to_bin(b.pos + b.rect.start)) + res_bin := _hash_bin(_world_to_bin(new_pos + b.rect.start)) if prev_bin != res_bin { - remove_from_bins(b^) + _remove_from_bins(b^) b.pos = new_pos - add_to_bins(b) + _add_to_bins(b) } else { b.pos = new_pos } |
