#! Contains continuous collision, as in a plane: a dynamic body that moved #! further in a step than half the least its shapes reach is swept, from where #! it began the step to where it ended it, against what stands still, a mesh's #! triangles and a heightfield's among it, and put back where it first comes #! within the slop of what it met, its velocity kept, so the next step's #! contacts stop it there. A bullet is swept against moving bodies too, where #! they ended the step. import std::containers; import std::math; import malunal::geometry; import malunal::linear; module malunal::physics3d; # A body moving fast enough this step to pass through something thin, and how # it moved. struct fast_body { var body: u32; var moved: sweep; } # Notes a body that moved fast enough to be swept, as the step ends. def space::note_fast(&mut this, b: u32, moved: sweep): void { val body := &this.bodies[b as u64]; return if !this.continuous || body.kind != body_kind::dynamic || body.least <= 0; val travel := (moved.c2 - moved.c1).length + moved.turning() * body.extent; if travel > 0.5 * body.least { this.fast.append(fast_body{ body = b, moved = moved }); } } # Sweeps the fast bodies, bullets last, so they meet the rest where they end # up, and places their colliders. def space::sweep_fast(&mut this): void { var fast := move(this.fast); for pass in 0..2u { for i in 0..fast.length { val bullet := this.bodies[fast[i].body as u64].bullet; continue if bullet != (pass == 1); this.sweep_body(&fast[i]); this.place_colliders(fast[i].body); } } fast.clear(); this.fast = move(fast); } # Puts a fast body back where it first meets what it may pass through. def space::sweep_body(&mut this, f: &fast_body): void { val b := f.body; val bullet := this.bodies[b as u64].bullet; val target := this.slop; val tolerance := 0.25 * this.slop; var first: f32 = 1; var c := this.bodies[b as u64].colliders; while c != none { val own := &this.colliders[c as u64]; if !own.made.sensor && !own.is_mesh() { val box := own.made.shape.bounds(f.moved.at(0)).merged(own.made.shape.bounds(f.moved.at(1))); this.tree.query(box, def(leaf: u32, data: u64): bool { val other := &this.colliders[data]; return true if other.body == b || other.made.sensor; return true if (other.made.layers & own.made.mask) == 0 || (own.made.layers & other.made.mask) == 0; val held := &this.bodies[other.body as u64]; return true if held.kind != body_kind::static && (!bullet || held.bullet); return true if this.joined_apart(b, other.body); val still := sweep::still(held.at); if other.is_mesh() { val surface := &this.meshes[other.mesh as u64]; val start := f.moved.at(0); val end := f.moved.at(1); surface.triangles_in(local_box(box, held.at), def(t: u32): bool { val corners := surface.triangle(t); # Only a triangle whose plane it reaches by the step's end, from # in front of it, may it pass through. val n := held.at.rotate(surface.normal(t)); val offset := n.dot(held.at.apply(corners[0])); return true if -reach_along(&own.made.shape, end, -n) - offset > target; return true if reach_along(&own.made.shape, start, n) - offset < 0; val face := shape::of_hull(hull::triangle(corners[0], corners[1], corners[2])); val at := impact_of(&face, &still, &own.made.shape, &f.moved, target, tolerance); if at > 0 && at < first { first = at; } return true; }); return true; } val at := impact_of(&other.made.shape, &still, &own.made.shape, &f.moved, target, tolerance); if at > 0 && at < first { first = at; } return true; }); } c = this.colliders[c as u64].next; } return if first >= 1; val at := f.moved.at(first); this.bodies[b as u64].at = at; this.bodies[b as u64].center = at.apply(this.bodies[b as u64].local_center); } # When a fast shape first meets what stands still: where they touch as the # step begins, when a small sphere about the fast shape's middle first meets # it, so its middle at least never passes through, as Box2D finds it. def impact_of(still_shape: &shape, still: &sweep, fast: &shape, moved: &sweep, target: f32, tolerance: f32): f32 { val t := time_of_impact(still_shape, still, fast, moved, target, tolerance); return t if t > 0; val core := shape::sphere(0.25 * fast.least_reach(), fast.middle()); return time_of_impact(still_shape, still, &core, moved, target, tolerance); } # A box of space as one of a pose's own places, holding it however it's # turned. def local_box(box: bounds, at: pose): bounds { var made := bounds::blank; for i in 0..8u { val corner := vec3{ (i & 1) != 0 ? box.max.x : box.min.x, (i & 2) != 0 ? box.max.y : box.min.y, (i & 4) != 0 ? box.max.z : box.min.z }; made = made.including(at.unapply(corner)); } return made; }