feat(ludic.physics): phys_own - a body owns a shape made for it alone, and phys_remove frees it with the body

A chunk's trunks and boulders put in and taken out made a new shape each time and freed none. The
table is by the body's index, made once at the world's size. tests/reuse_test: 1000 owned bodies put
and removed hold no more shapes and 32 bytes of heap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-28 13:52:07 +03:00
parent 404ee2ee3b
commit 5cd53d4024
6 changed files with 54 additions and 1 deletions

View file

@ -39,4 +39,25 @@ program ReuseTest {
expect_eq(phys_shapes(physics_st), 1)
phys_close(physics_st)
}
test "a body that owns its shape takes the shape with it, a thousand times over" (physics_st: mut PhysicsState) {
expect(phys_open(physics_st, 64, 1))
let s0 = phys_cylinder(physics_st, 5.0, 0.3)
phys_own(physics_st, phys_static_add(physics_st, s0, 0.0, 0.0, 0.0, 0.0), s0)
let first = phys_static_add(physics_st, phys_cylinder(physics_st, 5.0, 0.3), 3.0, 0.0, 0.0, 0.0)
phys_remove(physics_st, first)
let before = Os.heap_bytes()
for k in 0 .. 1000 {
let s = phys_cylinder(physics_st, 5.0, 0.3)
let b = phys_static_add(physics_st, s, float(k % 10), 0.0, 5.0, 0.0)
phys_own(physics_st, b, s)
phys_remove(physics_st, b)
}
let grew = Os.heap_bytes() - before
print(`reuse: 1000 owned bodies put and removed, {phys_shapes(physics_st)} shapes held in {len(physics_st.ph_shapes)} places, heap {grew} bytes`)
expect(phys_shapes(physics_st) <= 2)
expect(len(physics_st.ph_shapes) <= 3)
expect(grew < long(1024))
phys_close(physics_st)
}
}