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

@ -0,0 +1,28 @@
# owned.ludic - a shape a body owns: made for that body alone (a trunk's pillar, a boulder's scaled
# hull, a dropped thing's box) and freed when the body is removed, so a still thing put in and taken out
# again and again - a chunk that comes and goes - holds no more shapes than are standing
const PH_INDEX: int = 8388607 # a body id's low 23 bits are its index (Jolt's BodyID)
# every body owns nothing: at a world's opening, the table made once at its size and kept after
function ph_own_reset(physics_st: mut PhysicsState, max_bodies: int) -> void {
if len(physics_st.ph_own) < max_bodies {
List.clear(physics_st.ph_own)
for i in 0 .. max_bodies { push(physics_st.ph_own, -1) }
return
}
for i in 0 .. len(physics_st.ph_own) { physics_st.ph_own[i] = -1 }
}
# body `id` owns `shape`: phys_remove frees it with the body
export function phys_own(physics_st: mut PhysicsState, id: int, shape: int) -> void {
if id < 0 or shape < 0 { return }
let i = id & PH_INDEX
if i < len(physics_st.ph_own) { physics_st.ph_own[i] = shape }
}
function ph_own_free(physics_st: mut PhysicsState, id: int) -> void {
let i = id & PH_INDEX
if i >= len(physics_st.ph_own) or physics_st.ph_own[i] < 0 { return }
phys_shape_free(physics_st, physics_st.ph_own[i])
physics_st.ph_own[i] = -1
}