#! Contains how a space's bodies sleep: those that touch, or a joint holds #! together, are an island, found anew each step, and an island sleeps when #! all its bodies have been still for half a second, so a settled pile costs #! nothing until something wakes it. A kinematic body sleeps alone, when it #! has been still as long. import std::containers; import std::math; import malunal::linear; module malunal::physics3d; # How long, in seconds, a body is still before it may sleep. const time_to_sleep: f32 = 0.5; # The first body of the island a body is of, each body passed on the way # pointed nearer it, so the next finding is shorter. def root_of(parent: &mut vector, b: u32): u32 { var x := b; while parent[x as u64] != x { val up := parent[parent[x as u64] as u64]; parent[x as u64] = up; x = up; } return x; } def join(parent: &mut vector, a: u32, b: u32): void { val ra := root_of(parent, a); val rb := root_of(parent, b); if ra != rb { parent[math::max(ra, rb) as u64] = math::min(ra, rb); } } # Finds the islands of awake dynamic bodies, and puts to sleep those all of # whose bodies have been still long enough. def space::sleep_islands(&mut this, dt: f32): void { var parent := move(this.islands); parent.clear(); parent.resize(this.bodies.length, none); for b in 0..this.bodies.length { val body := &this.bodies[b]; if body.alive && body.awake && body.kind == body_kind::dynamic { parent[b] = b as u32; } } for k in 0..this.contacts.length { val c := &this.contacts[k]; continue if !c.alive || !c.touching; val a := c.body_a; val b := c.body_b; if parent[a as u64] != none && parent[b as u64] != none { join(&mut parent, a, b); } else if parent[a as u64] != none && this.bodies[b as u64].kind == body_kind::kinematic { this.keep_awake_on(a, b); } else if parent[b as u64] != none && this.bodies[a as u64].kind == body_kind::kinematic { this.keep_awake_on(b, a); } } this.join_jointed(&mut parent); # Each island's least time still, kept at its root. for b in 0..this.bodies.length { continue if parent[b] == none; val root := root_of(&mut parent, b as u32) as u64; this.bodies[root].sleep_time = math::min(this.bodies[root].sleep_time, this.bodies[b].sleep_time); } for b in 0..this.bodies.length { continue if parent[b] == none; val root := root_of(&mut parent, b as u32); continue if this.bodies[root as u64].sleep_time < time_to_sleep; # Linked after its root, which heads the group. this.bodies[b].awake = false; this.bodies[b].velocity = vec3::zero; this.bodies[b].spin = vec3::zero; this.bodies[b].group = root; if b as u32 != root { this.bodies[b].next_in_group = this.bodies[root as u64].next_in_group; this.bodies[root as u64].next_in_group = b as u32; } } # A kinematic body still long enough sleeps alone. for b in 0..this.bodies.length { val body := &this.bodies[b]; if body.alive && body.awake && body.kind == body_kind::kinematic && body.sleep_time >= time_to_sleep { this.bodies[b].awake = false; this.bodies[b].group = b as u32; } } this.islands = move(parent); } # Keeps a dynamic body awake where it rests on a kinematic body that moves. def space::keep_awake_on(&mut this, b: u32, kinematic: u32): void { val k := &this.bodies[kinematic as u64]; if k.velocity.length_squared > 0 || k.spin.length_squared > 0 { this.bodies[b as u64].sleep_time = 0; } } # Wakes a body, and every body of the group it sleeps with, and marks it as # having been still for no time. def space::wake_body(&mut this, b: u32): void { return if this.bodies[b as u64].kind == body_kind::static; this.bodies[b as u64].sleep_time = 0; return if this.bodies[b as u64].awake; var m := this.bodies[b as u64].group; if m == none { this.bodies[b as u64].awake = true; return; } while m != none { val next := this.bodies[m as u64].next_in_group; this.bodies[m as u64].awake = true; this.bodies[m as u64].sleep_time = 0; this.bodies[m as u64].group = none; this.bodies[m as u64].next_in_group = none; m = next; } }