#! Contains how far apart two shapes are, their nearest places found as #! Gilbert, Johnson, and Keerthi's GJK finds them; a shape cast along a motion, #! to where it first meets another; and the time of impact of two shapes each #! swept through a step, moving and turning, as continuous collision asks. #! #! GJK finds the place of the shapes' difference, every place of the second #! less every place of the first, nearest the origin, by a triangle of its #! corners at most, each step adding the corner furthest toward the origin and #! keeping only what the nearest place lies on, as Box2D's distance does. It #! works each shape's core, its points without their radius, then takes the #! radii off, so a circle's nearest place is as exact as a box's. #! #! A cast, and a time of impact, advance conservatively, as Mirtich's do: each #! moves the shapes as far as the gap between them, over how fast they can #! close it, says they can't yet meet, so neither ever passes through what it #! meets. import std::math; import malunal::linear; module malunal::physics2d; #! How far apart two shapes are, and the places of each nearest the other. pub struct gap { #! The places of the first's surface and the second's nearest one another: #! one place, midway, where they meet. pub var on_a: vec2; pub var on_b: vec2; #! The direction from the first toward the second, of length 1, or none #! where they meet. pub var normal: vec2; #! How far apart their surfaces are: 0 where they meet. pub var distance: f32; } #! Where a shape cast along a motion first met another: how far along the #! motion, as a fraction of it, the place of the other's surface it met, and #! which way that surface faces, toward the shape cast. pub struct cast_hit { pub var fraction: f32; pub var point: vec2; pub var normal: vec2; } # A corner of the shapes' difference, of a point of each, and its weight in # the nearest place. struct corner { var on_a: vec2; var on_b: vec2; var w: vec2; var weight: f32; var ia: u32; var ib: u32; } # As many as three corners, the nearest place lying among them. struct simplex { var corners: corner[3]; var count: u32; } # The point of a shape furthest along a direction of the plane, where a pose # places it. def support_of(s: &shape, at: &pose, direction: vec2): u32 { val d := at.turn.unapply(direction); var best: u32 = 0; var most := s.point(0).dot(d); for i in 1..s.count { val reach := s.point(i).dot(d); if reach > most { most = reach; best = i; } } return best; } def corner_of(a: &shape, at_a: &pose, ia: u32, b: &shape, at_b: &pose, ib: u32): corner { val pa := at_a.apply(a.point(ia)); val pb := at_b.apply(b.point(ib)); return corner{ on_a = pa, on_b = pb, w = pb - pa, weight = 1, ia = ia, ib = ib }; } # The nearest place of a segment of the difference to the origin, keeping only # the corner it's at where it's at one. def solve2(x: &mut simplex): void { val w1 := x.corners[0].w; val w2 := x.corners[1].w; val e12 := w2 - w1; val d12_2 := -w1.dot(e12); if d12_2 <= 0 { x.corners[0].weight = 1; x.count = 1; return; } val d12_1 := w2.dot(e12); if d12_1 <= 0 { x.corners[0] = x.corners[1]; x.corners[0].weight = 1; x.count = 1; return; } val inv := 1 / (d12_1 + d12_2); x.corners[0].weight = d12_1 * inv; x.corners[1].weight = d12_2 * inv; x.count = 2; } # The same of a triangle, by which of its corners, edges, or inside the # origin's nearest place is in, as Box2D's distance finds it. def solve3(x: &mut simplex): void { val w1 := x.corners[0].w; val w2 := x.corners[1].w; val w3 := x.corners[2].w; val e12 := w2 - w1; val d12_1 := w2.dot(e12); val d12_2 := -w1.dot(e12); val e13 := w3 - w1; val d13_1 := w3.dot(e13); val d13_2 := -w1.dot(e13); val e23 := w3 - w2; val d23_1 := w3.dot(e23); val d23_2 := -w2.dot(e23); val n123 := e12.cross(e13); val d123_1 := n123 * w2.cross(w3); val d123_2 := n123 * w3.cross(w1); val d123_3 := n123 * w1.cross(w2); if d12_2 <= 0 && d13_2 <= 0 { x.corners[0].weight = 1; x.count = 1; return; } if d12_1 > 0 && d12_2 > 0 && d123_3 <= 0 { val inv := 1 / (d12_1 + d12_2); x.corners[0].weight = d12_1 * inv; x.corners[1].weight = d12_2 * inv; x.count = 2; return; } if d13_1 > 0 && d13_2 > 0 && d123_2 <= 0 { val inv := 1 / (d13_1 + d13_2); x.corners[0].weight = d13_1 * inv; x.corners[1] = x.corners[2]; x.corners[1].weight = d13_2 * inv; x.count = 2; return; } if d12_1 <= 0 && d23_2 <= 0 { x.corners[0] = x.corners[1]; x.corners[0].weight = 1; x.count = 1; return; } if d13_1 <= 0 && d23_1 <= 0 { x.corners[0] = x.corners[2]; x.corners[0].weight = 1; x.count = 1; return; } if d23_1 > 0 && d23_2 > 0 && d123_1 <= 0 { val inv := 1 / (d23_1 + d23_2); x.corners[0] = x.corners[2]; x.corners[0].weight = d23_2 * inv; x.corners[1].weight = d23_1 * inv; x.count = 2; return; } val whole := d123_1 + d123_2 + d123_3; # A triangle of no area, its corners in a line: none holds the origin. if !(whole > 0) { x.corners[0].weight = 1; x.count = 1; return; } val inv := 1 / whole; x.corners[0].weight = d123_1 * inv; x.corners[1].weight = d123_2 * inv; x.corners[2].weight = d123_3 * inv; x.count = 3; } # Which way the next corner is looked for: toward the origin from the nearest # corner, or across the nearest edge, on the origin's side. def search_direction(x: &simplex): vec2 { return x.corners[0].w * -1f if x.count == 1; val e12 := x.corners[1].w - x.corners[0].w; return e12.perpendicular() if e12.cross(x.corners[0].w * -1f) > 0; return vec2{ e12.y, -e12.x }; } #! How far apart two shapes are, each where its pose places it, and the places #! of each nearest the other: none apart where they meet. pub def gap_between(a: &shape, at_a: pose, b: &shape, at_b: pose): gap { var x := simplex{ count = 1 }; x.corners[0] = corner_of(a, &at_a, 0, b, &at_b, 0); var saved_a: u32[3] = [0, 0, 0]; var saved_b: u32[3] = [0, 0, 0]; for iteration in 0..20u { val saved := x.count; for i in 0..saved { saved_a[i] = x.corners[i].ia; saved_b[i] = x.corners[i].ib; } if x.count == 2 { solve2(&mut x); } else if x.count == 3 { solve3(&mut x); } # Three corners hold the origin: the cores overlap. if x.count == 3 { break; } val d := search_direction(&x); if d.length_squared < tiny * tiny { break; } val ia := support_of(a, &at_a, d * -1f); val ib := support_of(b, &at_b, d); # A corner it has already found makes no progress. var again := false; for i in 0..saved { if saved_a[i] == ia && saved_b[i] == ib { again = true; } } if again { break; } x.corners[x.count] = corner_of(a, &at_a, ia, b, &at_b, ib); x.count++; } # The corner added last solved with the rest, where the passes ran out. if x.count == 2 { solve2(&mut x); } else if x.count == 3 { solve3(&mut x); } var on_a := vec2::zero; var on_b := vec2::zero; for i in 0..x.count { on_a = on_a + x.corners[i].on_a * x.corners[i].weight; on_b = on_b + x.corners[i].on_b * x.corners[i].weight; } if x.count == 3 { on_b = on_a; } val between := on_b - on_a; val cores := between.length; val reach := a.radius + b.radius; if cores > reach && cores > tiny { val normal := between * (1 / cores); return gap{ on_a = on_a + normal * a.radius, on_b = on_b - normal * b.radius, normal = normal, distance = cores - reach }; } # They meet: midway between their surfaces along the line between their # cores, where they have one. val normal := cores > tiny ? between * (1 / cores) : vec2::zero; val middle := ((on_a + normal * a.radius) + (on_b - normal * b.radius)) * 0.5; return gap{ on_a = middle, on_b = middle, normal = normal, distance = 0 }; } #! Where a shape, `b`, cast along a motion of the plane from where its pose #! places it, first meets another, `a`, which stays where its pose places it: #! within `tolerance` of its surface, a little short of touching, so what's #! moved there doesn't overlap. Nothing where it doesn't meet it within the #! motion; a fraction of 0 where they meet as it begins, its normal back along #! the motion. pub def shape_cast(a: &shape, at_a: pose, b: &shape, at_b: pose, motion: vec2, tolerance: f32 = 0.0025): maybe { val target := 0.5 * tolerance; var t: f32 = 0; var at := at_b; for iteration in 0..30u { val g := gap_between(a, at_a, b, at); if g.distance <= tolerance { val back := g.distance > 0 ? g.normal : motion.normalized() * -1f; return cast_hit{ fraction = t, point = g.on_a, normal = back }; } val approach := -motion.dot(g.normal); return nothing{} if approach <= tiny; t += (g.distance - target) / approach; return nothing{} if t > 1; at = pose{ at = at_b.at + motion * t, turn = at_b.turn }; } val g := gap_between(a, at_a, b, at); return cast_hit{ fraction = t, point = g.on_a, normal = g.normal }; } #! How a body moves through a step: its center of mass, of its own places, #! where that center begins and ends the step, and how it's turned as it begins #! and ends it, turning the shorter way between. pub struct sweep { pub var local_center: vec2; pub var c1: vec2; pub var c2: vec2; pub var q1 := rot::identity; pub var q2 := rot::identity; } #! A sweep standing still where a pose places a body. pub def sweep::still(at: pose, local_center: vec2 = vec2::zero): sweep { val center := at.apply(local_center); return sweep{ local_center = local_center, c1 = center, c2 = center, q1 = at.turn, q2 = at.turn }; } #! Where it places its body a fraction of the way through the step. pub def sweep::at(&this, t: f32): pose { val turn := rot::of(angle_between(this.q2, this.q1) * t) * this.q1; val center := this.c1.lerp(this.c2, t); return pose{ at = center - turn.apply(this.local_center), turn = turn }; } # How far a shape's surface reaches from a place of its own. def reach_from(s: &shape, center: vec2): f32 { var most: f32 = 0; for i in 0..s.count { most = math::max(most, (s.point(i) - center).length); } return most + s.radius; } #! When, as a fraction of the step, two shapes each swept through it first come #! within `target` of one another, to within `tolerance`, as their sweeps move #! and turn them: 1 where they don't. Each moves no further than they can, as #! fast as they close and turn, without meeting, so what's found is never past #! where they meet. pub def time_of_impact(a: &shape, sa: &sweep, b: &shape, sb: &sweep, target: f32, tolerance: f32): f32 { val turn_a := math::abs(angle_between(sa.q2, sa.q1)) * reach_from(a, sa.local_center); val turn_b := math::abs(angle_between(sb.q2, sb.q1)) * reach_from(b, sb.local_center); val moved := (sb.c2 - sb.c1) - (sa.c2 - sa.c1); var t: f32 = 0; for iteration in 0..50u { val g := gap_between(a, sa.at(t), b, sb.at(t)); return t if g.distance <= target + tolerance; val closing := -moved.dot(g.normal) + turn_a + turn_b; return 1 if closing <= tiny; t += (g.distance - target) / closing; return 1 if t >= 1; } return t; } # How far a shape reaches at least from its middle: a circle's or a capsule's # radius, or the nearest of a polygon's edges, as a body may move without # passing through what's in its way. def shape::least_reach(&this): f32 { return this.radius if this.kind != shape_kind::polygon; var middle := vec2::zero; for i in 0..this.count { middle = middle + this.point(i); } middle = middle * (1 / this.count as f32); var least := 1e30f; for i in 0..this.count { least = math::min(least, this.normal(i).dot(this.point(i) - middle)); } return least + this.radius; }