ludic/packages/ludic.physics/system.ludic
Orkuncakilkaya b4af12025d physics: Jolt's floor sized for what moves - pairs 4096, contacts 2048 and a 4 MB temp heap by default (phys_open_sized for more), and a body past max_bodies or a step past the pairs or contacts is Mem.over, never a quiet -1; world.ludic split from shapes.ludic
An empty world at the game's size held 51839 KB of Jolt's heap (200000 bodies, 65536 pairs, 20480
contacts, a 32 MB temp heap), the jolt= floor under every walk. Pairs and contacts come from what
moves - drops, boats, a few walkers - and a valley's still things make none. Now 11743 KB at 200000
bodies and 8461 KB at the 98304 the game opens for (jolt_floor_test holds it under 16 MB). The temp heap
still falls back to malloc for a step that wants more.

lib/macos-arm64 rebuilt; lib/windows-x64 needs native/build.sh on the PC (jph_world_new's new sizes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-29 16:18:42 +03:00

82 lines
3.4 KiB
Text

# system.ludic - the world stepped at a fixed rate in PH_SIMULATE, so frame rate never changes
# the rules. It saves nothing: a thing at rest is saved by its owner and the world rebuilt on load.
# as many fixed steps as the frame's time holds, at most ph_max_steps (a stall is not replayed)
export function phys_tick(physics_st: mut PhysicsState, t: Tick) -> void { phys_advance(physics_st, t.dt) }
export function phys_advance(physics_st: mut PhysicsState, dt: float) -> int {
if physics_st.ph_world == null { return 0 }
physics_st.ph_acc += dt
var n = 0
# a thousandth of a step of slack: 0.05 s is three steps even when float says 2.9999
while physics_st.ph_acc + physics_st.ph_step * 0.001 >= physics_st.ph_step and n < physics_st.ph_max_steps {
phys_step(physics_st)
physics_st.ph_acc -= physics_st.ph_step
n += 1
}
if physics_st.ph_acc > physics_st.ph_step or physics_st.ph_acc < 0.0 { physics_st.ph_acc = 0.0 }
return n
}
# exactly one step: the water, Jolt, then the contacts that matter as facts
export function phys_step(physics_st: mut PhysicsState) -> void {
if physics_st.ph_world == null { return }
ph_buffers()
ph_buoy_all(physics_st)
# 25.5a: Jolt's pairs or contacts full (PH_PAIRS, PH_CONTACTS) is a failure: past it things pass through
if jph_world_step(physics_st.ph_world, physics_st.ph_step, physics_st.ph_sub) != 0 { Mem.over("a physics step past phys_open's pairs or contacts") }
physics_st.ph_steps += 1
ph_contacts(physics_st)
}
export function phys_steps(physics_st: PhysicsState) -> int { return physics_st.ph_steps }
# m/s2 down is negative
export function phys_gravity(physics_st: mut PhysicsState, y: float) -> void { if physics_st.ph_world != null { jph_world_gravity(physics_st.ph_world, y) } }
# a fact record no drain holds any more, made fresh (and kept) only when every one is held: a
# fact can come every frame, and a record a frame is never given back
function ph_fact_record(physics_st: mut PhysicsState) -> PhysFact {
if physics_st.ph_fnext >= len(physics_st.ph_ffree) {
q_unheld(phys_facts(physics_st), physics_st.ph_fpool, physics_st.ph_ffree)
physics_st.ph_fnext = 0
}
if physics_st.ph_fnext < len(physics_st.ph_ffree) {
let f = physics_st.ph_ffree[physics_st.ph_fnext]
physics_st.ph_fnext += 1
return f
}
return ph_fact_more(physics_st)
}
function ph_contacts(physics_st: mut PhysicsState) -> void {
let n = jph_contacts_drain(physics_st.ph_world, physics_st.ph_ids, physics_st.ph_vals, PH_CAP)
for i in 0 .. n {
let speed = physics_st.ph_vals[i * 4 + 3]
if speed >= physics_st.ph_hard {
let f = ph_fact_record(physics_st)
f.what = PHYS_HIT
f.a = physics_st.ph_ids[i * 2]
f.b = physics_st.ph_ids[i * 2 + 1]
f.x = physics_st.ph_vals[i * 4]
f.y = physics_st.ph_vals[i * 4 + 1]
f.z = physics_st.ph_vals[i * 4 + 2]
f.speed = speed
q_push(phys_facts(physics_st), f)
}
}
}
# contacts C had to drop because the buffer was full between two drains
export function phys_contacts_lost(physics_st: PhysicsState) -> int {
if physics_st.ph_world == null { return 0 }
return jph_contacts_lost(physics_st.ph_world)
}
export function phys_reset(physics_st: mut PhysicsState) -> void { physics_st.ph_acc = 0.0 }
export function phys_system() -> System {
let s = system_new("physics", PH_SIMULATE)
s.reset = fn phys_reset
s.tick = fn phys_tick
return s
}