aboutsummaryrefslogtreecommitdiff
path: root/src/phys
diff options
context:
space:
mode:
Diffstat (limited to 'src/phys')
-rw-r--r--src/phys/body.odin3
-rw-r--r--src/phys/world.odin77
2 files changed, 39 insertions, 41 deletions
diff --git a/src/phys/body.odin b/src/phys/body.odin
index fcae697..0256dee 100644
--- a/src/phys/body.odin
+++ b/src/phys/body.odin
@@ -37,7 +37,6 @@ Body :: struct {
}
make_body :: proc(
- w: ^World,
rect: Rect,
layers := bit_set[Layer; u16]{.DEFAULT},
mask := bit_set[Layer; u16]{.DEFAULT},
@@ -48,7 +47,7 @@ make_body :: proc(
mask = mask,
active = true,
}
- return add_body(w, b)
+ return add_body(b)
}
aabb_hori :: proc(a: Rect, b: Rect) -> bool {
diff --git a/src/phys/world.odin b/src/phys/world.odin
index eac3d5c..3154a0b 100644
--- a/src/phys/world.odin
+++ b/src/phys/world.odin
@@ -3,22 +3,22 @@ package phys
import "core:log"
import "core:math"
-import sapp "shared:sokol/app"
+import rl "vendor:raylib"
BIN_COUNT :: 2056
BIN_SIZE :: 64
Body_Handle :: u32
-World :: struct {
+world: struct {
handles: [dynamic]u32,
unused_handles: [dynamic]Body_Handle,
bodies: [dynamic]Body,
bins: [BIN_COUNT][dynamic]Body_Handle,
}
-destroy_world :: proc(w: World) {
- for bin in w.bins {
+destroy_world :: proc() {
+ for bin in world.bins {
delete(bin)
}
}
@@ -37,7 +37,6 @@ world_to_bin :: proc(point: Vec2) -> (i32, i32) {
@(private="file")
get_surrounding_bins :: proc(
- w: ^World,
pos: Vec2,
allocator := context.temp_allocator,
) -> []^[dynamic]Body_Handle {
@@ -50,7 +49,7 @@ get_surrounding_bins :: proc(
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 := &w.bins[bin_idx % BIN_COUNT]
+ bin := &world.bins[bin_idx % BIN_COUNT]
neighbors[idx] = bin
idx += 1
}
@@ -60,79 +59,79 @@ get_surrounding_bins :: proc(
}
@(private="file")
-find_bin :: proc(w: ^World, b: Body) -> ^[dynamic]Body_Handle {
+find_bin :: proc(b: Body) -> ^[dynamic]Body_Handle {
bin_x, bin_y := world_to_bin(b.pos + b.rect.start)
- return &w.bins[hash_bin(bin_x, bin_y) % BIN_COUNT]
+ return &world.bins[hash_bin(bin_x, bin_y) % BIN_COUNT]
}
@(private="file")
-add_to_bins :: proc(w: ^World, b: Body) {
- bin := find_bin(w, b)
+add_to_bins :: proc(b: Body) {
+ bin := find_bin(b)
idx := i32(len(bin))
append(bin, b.handle)
- w.bodies[bin[idx]].bin_idx = idx
+ world.bodies[bin[idx]].bin_idx = idx
}
@(private="file")
-remove_from_bins :: proc(w: ^World, b: Body) {
- bin := find_bin(w, b)
+remove_from_bins :: proc(b: Body) {
+ bin := find_bin(b)
assert(bin[b.bin_idx] == b.handle)
last := pop(bin)
if last != b.handle {
bin[b.bin_idx] = last
- w.bodies[last].bin_idx = b.bin_idx
+ world.bodies[last].bin_idx = b.bin_idx
}
}
-get_body :: proc(w: World, h: Body_Handle) -> ^Body {
- return &w.bodies[w.handles[h]]
+get_body :: proc(h: Body_Handle) -> ^Body {
+ return &world.bodies[world.handles[h]]
}
-add_body :: proc(w: ^World, b: Body) -> (Body_Handle, ^Body) {
+add_body :: proc(b: Body) -> (Body_Handle, ^Body) {
handle: Body_Handle
if b.rect.size.x > BIN_SIZE || b.rect.size.y > BIN_SIZE {
log.warnf("Body size is too big (%vx%v)", b.rect.size.x, b.rect.size.y)
}
- if len(w.unused_handles) > 0 {
- handle = pop(&w.unused_handles)
+ if len(world.unused_handles) > 0 {
+ handle = pop(&world.unused_handles)
} else {
- handle = cast(Body_Handle)len(w.handles)
- append(&w.handles, 0)
+ handle = cast(Body_Handle)len(world.handles)
+ append(&world.handles, 0)
}
- w.handles[handle] = u32(len(w.bodies))
- append(&w.bodies, b)
+ world.handles[handle] = u32(len(world.bodies))
+ append(&world.bodies, b)
- body := &w.bodies[w.handles[handle]]
+ body := &world.bodies[world.handles[handle]]
body.handle = handle
- add_to_bins(w, body^)
+ add_to_bins(body^)
return handle, body
}
-remove_body :: proc(w: ^World, h: Body_Handle) {
- b := get_body(w^, h)
+remove_body :: proc(h: Body_Handle) {
+ b := get_body(h)
- remove_from_bins(w, b^)
+ remove_from_bins(b^)
- last := pop(&w.bodies)
+ last := pop(&world.bodies)
if last.handle != h {
- w.bodies[h] = last
- w.handles[last.handle] = w.handles[h]
+ world.bodies[h] = last
+ world.handles[last.handle] = world.handles[h]
}
- append(&w.unused_handles, b.handle)
+ append(&world.unused_handles, b.handle)
}
-update_body :: proc(w: ^World, h: Body_Handle) {
- dt := f32(sapp.frame_duration())
+update_body :: proc(h: Body_Handle) {
+ dt := rl.GetFrameTime()
- b := get_body(w^, h)
+ b := get_body(h)
res_pos := b.pos + b.vel * dt
@@ -142,7 +141,7 @@ update_body :: proc(w: ^World, h: Body_Handle) {
res_rect := b.rect
res_rect.start += res_pos
- bin_list := get_surrounding_bins(w, res_rect.start)
+ bin_list := get_surrounding_bins(res_rect.start)
b.collisions = {}
@@ -156,7 +155,7 @@ update_body :: proc(w: ^World, h: Body_Handle) {
continue
}
- c := get_body(w^, c_h)
+ c := get_body(c_h)
c_rect := c.rect
c_rect.start += c.pos
@@ -198,8 +197,8 @@ update_body :: proc(w: ^World, h: Body_Handle) {
res_bin := hash_bin(world_to_bin(res_pos + b.rect.start))
if prev_bin != res_bin {
- remove_from_bins(w, b^)
- add_to_bins(w, b^)
+ remove_from_bins(b^)
+ add_to_bins(b^)
}
}