#! Contains how a space finds which colliders are near one another, by the #! boxes of its tree, and where those meet, as contacts kept from one step to #! the next, so each begins with the impulses that held its bodies apart last. #! Two shapes have one contact; a shape and a grid one for each solid cell they #! meet, named by the cell, so it's kept as they slide. import std::containers; import std::math; import malunal::linear; module malunal::physics2d; #! Two colliders that began or stopped touching in a step: sensors among #! them, which touch where they overlap. pub struct contact_event { pub var a: collider_id; pub var b: collider_id; #! Whether they began touching, or stopped. pub var began: bool; #! Whether either is a sensor. pub var sensor: bool; } # Two colliders whose grown boxes meet, the first's index the lesser, and # their contacts, a list of them: none of sensors, which only note whether # they overlap. struct pair_record { var a: u32; var b: u32; var contacts: u32 = 0xFFFFFFFF; var touching: bool; var sensor: bool; var grid: bool; var alive: bool; } # Where two colliders meet, or a shape and one cell of a grid: the manifold, # and the impulses that held them apart at each of its points last step. struct contact_record { var pair: u32; var child: u32; # The cell, of a grid's. var next: u32 = 0xFFFFFFFF; var body_a: u32; var body_b: u32; var manifold: manifold; var normal_impulse: f32[2]; var tangent_impulse: f32[2]; var friction: f32; var restitution: f32; var touching: bool; var seen: bool; var alive: bool; } # A cell a shape met, and how. struct met_cell { var cell: u32; var manifold: manifold; } # The key of a pair of colliders, whichever comes first. def pair_key(a: u32, b: u32): u64 { val low := math::min(a, b) as u64; val high := math::max(a, b) as u64; return (low << 32) | high; } # Whether two colliders may meet: of two bodies, one of them dynamic, on # layers each other's masks hold, not both grids, and not of bodies a joint # keeps apart. def space::may_meet(&this, a: u32, b: u32): bool { val ca := &this.colliders[a as u64]; val cb := &this.colliders[b as u64]; return false if ca.body == cb.body || (ca.is_grid() && cb.is_grid()); return false if !this.bodies[ca.body as u64].is_dynamic() && !this.bodies[cb.body as u64].is_dynamic(); return false if (ca.made.layers & cb.made.mask) == 0 || (cb.made.layers & ca.made.mask) == 0; return !this.joined_apart(ca.body, cb.body); } # Pairs each collider whose leaf moved with those its grown box newly meets. def space::find_pairs(&mut this): void { return if this.moved.length == 0; var near := move(this.near); for m in 0..this.moved.length { val c := this.moved[m]; continue if !this.colliders[c as u64].alive; near.clear(); this.tree.query(this.tree.box_of(this.colliders[c as u64].leaf), def(leaf: u32, data: u64): bool { val other := data as u32; # Where both moved, the one of the lesser index pairs them. if other != c && !(this.colliders[data].moved && other < c) { near.append(other); } return true; }); for k in 0..near.length { val other := near[k]; val key := pair_key(c, other); if this.may_meet(c, other) && !this.pair_keys.contains(&key) { this.make_pair(math::min(c, other), math::max(c, other)); } } } for m in 0..this.moved.length { this.colliders[this.moved[m] as u64].moved = false; } this.moved.clear(); this.near = move(near); } def space::make_pair(&mut this, a: u32, b: u32): void { var p: u32 = 0; if this.free_pairs.length > 0 { p = this.free_pairs[this.free_pairs.length - 1]; this.free_pairs.truncate(); } else { p = this.pairs.length as u32; this.pairs.append(pair_record{}); } val sensor := this.colliders[a as u64].made.sensor || this.colliders[b as u64].made.sensor; val grid := this.colliders[a as u64].is_grid() || this.colliders[b as u64].is_grid(); this.pairs[p as u64] = pair_record{ a = a, b = b, sensor = sensor, grid = grid, alive = true }; this.pair_keys.set(pair_key(a, b), p); if !sensor && !grid { this.pairs[p as u64].contacts = this.make_contact(p, 0); } } # A new contact of a pair, put first of its list. def space::make_contact(&mut this, p: u32, child: u32): u32 { var k: u32 = 0; if this.free_contacts.length > 0 { k = this.free_contacts[this.free_contacts.length - 1]; this.free_contacts.truncate(); } else { k = this.contacts.length as u32; this.contacts.append(contact_record{}); } val a := this.pairs[p as u64].a; val b := this.pairs[p as u64].b; val ma := &this.colliders[a as u64].made; val mb := &this.colliders[b as u64].made; this.contacts[k as u64] = contact_record{ pair = p, child = child, next = this.pairs[p as u64].contacts, body_a = this.colliders[a as u64].body, body_b = this.colliders[b as u64].body, friction = math::sqrt(ma.friction * mb.friction), restitution = math::max(ma.restitution, mb.restitution), alive = true, }; return k; } def space::free_contact(&mut this, k: u32): void { this.contacts[k as u64].alive = false; this.free_contacts.append(k); } # Frees a pair and its contacts, noting it stopped where it touched, and # waking its bodies then where `wake` is given. def space::drop_pair(&mut this, p: u32, wake: bool): void { val pair := this.pairs[p as u64]; if pair.touching { this.note(p, false); if wake { this.wake_body(this.colliders[pair.a as u64].body); this.wake_body(this.colliders[pair.b as u64].body); } } var k := pair.contacts; while k != none { val next := this.contacts[k as u64].next; this.free_contact(k); k = next; } this.pair_keys.remove(&pair_key(pair.a, pair.b)); this.pairs[p as u64].alive = false; this.free_pairs.append(p); } # Notes that a pair began or stopped touching. def space::note(&mut this, p: u32, began: bool): void { val a := this.pairs[p as u64].a; val b := this.pairs[p as u64].b; this.noted.append(contact_event{ a = collider_id{ index = a, generation = this.colliders[a as u64].generation }, b = collider_id{ index = b, generation = this.colliders[b as u64].generation }, began = began, sensor = this.pairs[p as u64].sensor, }); } # Whether a body moves this step: dynamic or kinematic, and awake. def space::moves(&this, b: u32): bool { val body := &this.bodies[b as u64]; return body.kind != body_kind::static && body.awake; } # Finds where each pair of a moving body meets, dropping those whose grown # boxes no longer meet. def space::update_contacts(&mut this): void { for p in 0..this.pairs.length { continue if !this.pairs[p].alive; val a := this.pairs[p].a; val b := this.pairs[p].b; val ba := this.colliders[a as u64].body; val bb := this.colliders[b as u64].body; continue if !this.moves(ba) && !this.moves(bb); if !this.tree.box_of(this.colliders[a as u64].leaf).overlaps(this.tree.box_of(this.colliders[b as u64].leaf)) { this.drop_pair(p as u32, false); continue; } var touching := false; if this.pairs[p].sensor { touching = this.sensed(a, b); } else if this.pairs[p].grid { touching = this.update_grid_contacts(p as u32); } else { touching = this.update_contact(this.pairs[p].contacts, this.manifold_of(a, b)); } if touching != this.pairs[p].touching { this.pairs[p].touching = touching; this.note(p as u32, touching); } # What touches a sleeping body, moving, wakes it, as a lift that sets off # wakes what rests on it. if touching { this.wake_if_pushed(ba, bb); this.wake_if_pushed(bb, ba); } } } # Wakes a sleeping dynamic body where what touches it is awake and moves it: a # dynamic body, or a kinematic one moving. def space::wake_if_pushed(&mut this, b: u32, by: u32): void { return if this.bodies[b as u64].awake || this.bodies[b as u64].kind != body_kind::dynamic; val other := &this.bodies[by as u64]; if other.awake && (other.kind == body_kind::dynamic || other.velocity.length_squared > 0 || other.spin != 0) { this.wake_body(b); } } # Where two colliders' shapes meet, within the speculative margin. def space::manifold_of(&this, a: u32, b: u32): manifold { val ca := &this.colliders[a as u64]; val cb := &this.colliders[b as u64]; if collide(&ca.made.shape, this.bodies[ca.body as u64].at, &cb.made.shape, this.bodies[cb.body as u64].at, this.speculative) is some(m) { return m; } return manifold{}; } # Whether a sensor's pair overlaps: a shape and a shape, or a shape and a # grid. def space::sensed(&this, a: u32, b: u32): bool { val ca := &this.colliders[a as u64]; val cb := &this.colliders[b as u64]; val at_a := this.bodies[ca.body as u64].at; val at_b := this.bodies[cb.body as u64].at; return this.grids[ca.grid as u64].overlaps(at_a, &cb.made.shape, at_b) if ca.is_grid(); return this.grids[cb.grid as u64].overlaps(at_b, &ca.made.shape, at_a) if cb.is_grid(); return overlaps(&ca.made.shape, at_a, &cb.made.shape, at_b); } # Gives a contact a manifold, each point beginning with the impulses of last # step's point of the same features. # @returns Whether it touches, within the speculative margin. def space::update_contact(&mut this, k: u32, found: manifold): bool { val c := &mut this.contacts[k as u64]; var normal: f32[2] = [0, 0]; var tangent: f32[2] = [0, 0]; for i in 0..found.count { for j in 0..c.manifold.count { if found.points[i].id == c.manifold.points[j].id { normal[i] = c.normal_impulse[j]; tangent[i] = c.tangent_impulse[j]; } } } c.manifold = found; c.normal_impulse = normal; c.tangent_impulse = tangent; c.touching = found.count > 0; c.seen = true; return c.touching; } # Finds the solid cells a shape meets of a grid, a contact for each, keeping # those it met last step and dropping those it no longer meets. # @returns Whether any touches. def space::update_grid_contacts(&mut this, p: u32): bool { val a := this.pairs[p as u64].a; val b := this.pairs[p as u64].b; # The grid as the first, its normals from each cell toward the shape, # turned back where the shape is the pair's first. val grid_first := this.colliders[a as u64].is_grid(); val g := grid_first ? a : b; val s := grid_first ? b : a; var met := move(this.met); met.clear(); val held := &this.colliders[g as u64]; val shaped := &this.colliders[s as u64]; val cells := &this.grids[held.grid as u64]; val width := cells.width; cells.collide(this.bodies[held.body as u64].at, &shaped.made.shape, this.bodies[shaped.body as u64].at, def(x: u32, y: u32, found: manifold): bool { met.append(met_cell{ cell = y * width + x, manifold = grid_first ? found : found.flipped() }); return true; }, this.speculative); var k := this.pairs[p as u64].contacts; while k != none { this.contacts[k as u64].seen = false; k = this.contacts[k as u64].next; } var touching := false; for i in 0..met.length { var found := this.pairs[p as u64].contacts; while found != none && this.contacts[found as u64].child != met[i].cell { found = this.contacts[found as u64].next; } if found == none { found = this.make_contact(p, met[i].cell); this.pairs[p as u64].contacts = found; } if this.update_contact(found, met[i].manifold) { touching = true; } } this.met = move(met); # Those not met this step are dropped. var previous: u32 = none; k = this.pairs[p as u64].contacts; while k != none { val next := this.contacts[k as u64].next; if !this.contacts[k as u64].seen { if previous == none { this.pairs[p as u64].contacts = next; } else { this.contacts[previous as u64].next = next; } this.free_contact(k); } else { previous = k; } k = next; } return touching; }