#! Contains what a space is asked of the shapes in it: which colliders a shape #! overlaps, and where a shape cast along a motion first meets one, as a #! character's feet and a thrown stone ask. import std::containers; import std::math; import malunal::linear; module malunal::physics2d; #! Where a shape cast through a space first met one of its colliders: which, #! of which body, how far along the motion, as a fraction of it, where on what #! it met, and which way that faces, toward the shape cast. pub struct space_cast { pub var collider: collider_id; pub var body: body_id; pub var fraction: f32; pub var point: vec2; pub var normal: vec2; } #! What a query of a space passes over: colliders on no layer of its `mask`; #! sensors, unless `sensors`; and those whose data is `ignore`, where it isn't #! 0, as the asker's own, a body and its colliders given an entity's bits. pub struct query_filter { pub var mask: u32 = 0xFFFFFFFF; pub var sensors := false; pub var ignore: u64 = 0; } # Whether a query of a filter asks of a collider. def space::passes(&this, c: u32, filter: &query_filter): bool { val held := &this.colliders[c as u64].made; return (held.layers & filter.mask) != 0 && (!held.sensor || filter.sensors) && (filter.ignore == 0 || held.data != filter.ignore); } def space::named(&this, c: u32): collider_id { return collider_id{ index = c, generation = this.colliders[c as u64].generation }; } def space::body_named(&this, b: u32): body_id { return body_id{ index = b, generation = this.bodies[b as u64].generation }; } #! Visits each collider on a layer of a mask that a shape overlaps, where a #! pose places it, sensors passed over, as `visit(collider, body)`, until it #! gives false. pub def space::overlap(&this, s: &shape, at: pose, mask: u32, visit: F): void where F: invokable { this.overlap(s, at, query_filter{ mask = mask }, visit); } #! Visits each collider a shape overlaps, where a pose places it, that a #! filter asks of, as `visit(collider, body)`, until it gives false. pub def space::overlap(&this, s: &shape, at: pose, filter: query_filter, visit: F): void where F: invokable { this.tree.query(s.bounds(at), def(leaf: u32, data: u64): bool { val c := data as u32; return true if !this.passes(c, &filter); val held := &this.colliders[data]; val placed := this.bodies[held.body as u64].at; var met := false; if held.is_grid() { met = this.grids[held.grid as u64].overlaps(placed, s, at); } else { met = overlaps(&held.made.shape, placed, s, at); } return true if !met; return visit(this.named(c), this.body_named(held.body)); }); } #! Where a shape, cast along a motion of the plane from where a pose places #! it, first meets a collider on a layer of a mask, sensors passed over, and #! chains' segments and one-way colliders it begins behind: within a quarter of #! the slop of what it meets, a little short of touching. A fraction of 0 where #! it overlaps something as it begins. Colliders of `except`, a body of its #! own, as a character's, are passed over. pub def space::shape_cast(&this, s: &shape, at: pose, motion: vec2, mask: u32 = 0xFFFFFFFF, except: body_id = body_id{}): maybe { return this.shape_cast(s, at, motion, query_filter{ mask = mask }, except); } #! Where a shape, cast along a motion as `shape_cast` casts it, first meets a #! collider a filter asks of. pub def space::shape_cast(&this, s: &shape, at: pose, motion: vec2, filter: query_filter, except: body_id = body_id{}): maybe { var best: maybe = nothing{}; var first: f32 = 2; val tolerance := 0.25 * this.slop; val moved := pose{ at = at.at + motion, turn = at.turn }; val box := s.bounds(at).merged(s.bounds(moved)); this.tree.query(box, def(leaf: u32, data: u64): bool { val c := data as u32; return true if !this.passes(c, &filter) || this.behind_one_way(c, s, at) || this.colliders[data].body == except.index; val held := &this.colliders[data]; val placed := this.bodies[held.body as u64].at; if held.is_grid() { val cells := &this.grids[held.grid as u64]; cells.cells_in(local_box(box, placed), def(x: u32, y: u32): bool { val cell := cells.box_of(x, y); val solid := shape::box(cell.half_size, cell.center); val hit := shape_cast(&solid, placed, s, at, motion, tolerance) else { return true; }; if hit.fraction < first { first = hit.fraction; best = space_cast{ collider = this.named(c), body = this.body_named(held.body), fraction = hit.fraction, point = hit.point, normal = hit.normal }; } return true; }); return true; } val hit := shape_cast(&held.made.shape, placed, s, at, motion, tolerance) else { return true; }; if hit.fraction < first { first = hit.fraction; best = space_cast{ collider = this.named(c), body = this.body_named(held.body), fraction = hit.fraction, point = hit.point, normal = hit.normal }; } return true; }); return best; }