302 lines
11 KiB
Plaintext
302 lines
11 KiB
Plaintext
#! Contains collision between shapes: whether two meet, where they're placed,
|
|||
|
|
#! and the `manifold` of where and how deep, which way to push them apart.
|
||
|
|
#!
|
||
|
|
#! Each shape is taken as a hull, its points where its pose places them, with
|
||
|
|
#! its radius around them: a circle of one point, a capsule of two, a polygon
|
||
|
|
#! of its corners. Two circles meet as their middles near; a circle and a hull
|
||
|
|
#! as the circle's middle nears the hull's edges, or lies within it; and two
|
||
|
|
#! hulls by the edge of either that holds them furthest apart, as the
|
||
|
|
#! separating axis theorem finds it. Where their hulls overlap, or their round
|
||
|
|
#! sides near along a face, the other's nearest edge is clipped to that face,
|
||
|
|
#! as Box2D clips one, so a box resting flat on another meets it at two
|
||
|
|
#! points; where only their round sides meet, they meet at one, along the line
|
||
|
|
#! between the hulls' nearest points.
|
||
|
|
|
||
|
|
import std::math;
|
||
|
|
import malunal::linear;
|
||
|
|
module malunal::physics2d;
|
||
|
|
|
||
|
|
#! A point where two shapes meet: midway between their surfaces, how deep the
|
||
|
|
#! one is within the other there, 0 where they touch and below where they're
|
||
|
|
#! apart within a margin, and the features of each that meet there, which stay
|
||
|
|
#! the same as they slide, so a solver knows the point from one step to the
|
||
|
|
#! next.
|
||
|
|
pub struct contact {
|
||
|
|
pub var point: vec2;
|
||
|
|
pub var depth: f32;
|
||
|
|
pub var id: u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where and how deep two shapes meet: the normal from the first toward the
|
||
|
|
#! second, along which the second is pushed out of the first, and one or two
|
||
|
|
#! points, as a box resting flat on another rests on two.
|
||
|
|
pub struct manifold {
|
||
|
|
pub var normal: vec2;
|
||
|
|
pub var points: contact[2];
|
||
|
|
pub var count: u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The deepest of its points.
|
||
|
|
pub val manifold::depth: f32 {
|
||
|
|
get => this.count == 0 ? 0f : this.count == 1 ? this.points[0].depth : math::max(this.points[0].depth, this.points[1].depth);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The manifold turned around, of the second shape against the first.
|
||
|
|
pub def manifold::flipped(&this): manifold {
|
||
|
|
return manifold{ normal = this.normal * -1f, points = this.points, count = this.count };
|
||
|
|
}
|
||
|
|
|
||
|
|
# A shape's points where its pose places it, and its edges' normals.
|
||
|
|
struct hull {
|
||
|
|
var points: vec2[8];
|
||
|
|
var normals: vec2[8];
|
||
|
|
var count: u32;
|
||
|
|
var radius: f32;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Turned a number at a time, which sakurac keeps in registers.
|
||
|
|
def hull_of(s: &shape, at: pose): hull {
|
||
|
|
var made := hull{ count = s.count, radius = s.radius };
|
||
|
|
val c := at.turn.c;
|
||
|
|
val sn := at.turn.s;
|
||
|
|
for i in 0..s.count {
|
||
|
|
val p := s.point(i);
|
||
|
|
val n := s.normal(i);
|
||
|
|
made.points[i] = vec2{ at.at.x + c * p.x - sn * p.y, at.at.y + sn * p.x + c * p.y };
|
||
|
|
made.normals[i] = vec2{ c * n.x - sn * n.y, sn * n.x + c * n.y };
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# A shape's own points and normals, unturned.
|
||
|
|
def own_hull(s: &shape): hull {
|
||
|
|
var made := hull{ count = s.count, radius = s.radius };
|
||
|
|
for i in 0..s.count {
|
||
|
|
made.points[i] = s.point(i);
|
||
|
|
made.normals[i] = s.normal(i);
|
||
|
|
}
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where two shapes meet, each where its pose places it, or nothing where they
|
||
|
|
#! don't: the manifold's normal from the first toward the second. Where a
|
||
|
|
#! margin is given, shapes apart by no more than it meet too, at points of a
|
||
|
|
#! depth below 0, as a solver's speculative contacts are, which keep what falls
|
||
|
|
#! from passing into what's below it in one step.
|
||
|
|
pub def collide(a: &shape, at_a: pose, b: &shape, at_b: pose, margin: f32 = 0): maybe<manifold> {
|
||
|
|
# Worked among the first's own places, so only the second's points are
|
||
|
|
# turned, then the manifold turned back.
|
||
|
|
val found := collide_own(a, b, at_a.unapply_pose(at_b), margin) else { return nothing{}; };
|
||
|
|
val c := at_a.turn.c;
|
||
|
|
val sn := at_a.turn.s;
|
||
|
|
var placed := manifold{ normal = vec2{ c * found.normal.x - sn * found.normal.y, sn * found.normal.x + c * found.normal.y }, count = found.count };
|
||
|
|
for i in 0..found.count {
|
||
|
|
val p := found.points[i].point;
|
||
|
|
placed.points[i] = contact{ point = vec2{ at_a.at.x + c * p.x - sn * p.y, at_a.at.y + sn * p.x + c * p.y }, depth = found.points[i].depth, id = found.points[i].id };
|
||
|
|
}
|
||
|
|
return placed;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The same of the second placed among the first's own places.
|
||
|
|
def collide_own(a: &shape, b: &shape, at_b: pose, margin: f32): maybe<manifold> {
|
||
|
|
val ha := own_hull(a);
|
||
|
|
val hb := hull_of(b, at_b);
|
||
|
|
if ha.count == 1 && hb.count == 1 {
|
||
|
|
return circles(ha.points[0], ha.radius, hb.points[0], hb.radius, margin);
|
||
|
|
}
|
||
|
|
if hb.count == 1 {
|
||
|
|
return hull_and_circle(&ha, hb.points[0], hb.radius, margin);
|
||
|
|
}
|
||
|
|
if ha.count == 1 {
|
||
|
|
if hull_and_circle(&hb, ha.points[0], ha.radius, margin) is some(m) {
|
||
|
|
return m.flipped();
|
||
|
|
}
|
||
|
|
return nothing{};
|
||
|
|
}
|
||
|
|
return hulls(&ha, &hb, margin);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether two shapes meet, each where its pose places it, touching among it.
|
||
|
|
pub def overlaps(a: &shape, at_a: pose, b: &shape, at_b: pose): bool {
|
||
|
|
return collide(a, at_a, b, at_b) is some(_);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A manifold of one point, between the surfaces of the first, at `on_a`
|
||
|
|
# within `radius_a` of it along the normal, and the second's, at `on_b` less
|
||
|
|
# `radius_b`.
|
||
|
|
def one_point(normal: vec2, on_a: vec2, radius_a: f32, on_b: vec2, radius_b: f32, depth: f32): manifold {
|
||
|
|
var made := manifold{ normal = normal, count = 1 };
|
||
|
|
made.points[0] = contact{ point = ((on_a + normal * radius_a) + (on_b - normal * radius_b)) * 0.5, depth = depth };
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
def circles(a: vec2, ra: f32, b: vec2, rb: f32, margin: f32): maybe<manifold> {
|
||
|
|
val between := b - a;
|
||
|
|
val distance := between.length;
|
||
|
|
return nothing{} if distance > ra + rb + margin;
|
||
|
|
val normal := distance > tiny ? between * (1 / distance) : vec2{ 0, 1 };
|
||
|
|
return one_point(normal, a, ra, b, rb, ra + rb - distance);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A hull and a circle: within a polygon, pushed out by the face it's nearest;
|
||
|
|
# else by the hull's point nearest the circle's middle.
|
||
|
|
def hull_and_circle(h: &hull, c: vec2, radius: f32, margin: f32): maybe<manifold> {
|
||
|
|
val reach := h.radius + radius;
|
||
|
|
# The face the middle is furthest in front of.
|
||
|
|
var face: u32 = 0;
|
||
|
|
var separation := -1e30f;
|
||
|
|
for i in 0..h.count {
|
||
|
|
val s := h.normals[i].dot(c - h.points[i]);
|
||
|
|
if s > separation {
|
||
|
|
separation = s;
|
||
|
|
face = i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nothing{} if separation > reach + margin;
|
||
|
|
if h.count >= 3 && separation <= 0 {
|
||
|
|
val n := h.normals[face];
|
||
|
|
return one_point(n, c - n * separation, h.radius, c, radius, reach - separation);
|
||
|
|
}
|
||
|
|
var nearest := h.points[0];
|
||
|
|
var best := 1e30f;
|
||
|
|
for i in 0..h.count {
|
||
|
|
val p := nearest_on_segment(c, h.points[i], h.points[(i + 1) % h.count]);
|
||
|
|
val d := (c - p).length_squared;
|
||
|
|
if d < best {
|
||
|
|
best = d;
|
||
|
|
nearest = p;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val distance := math::sqrt(best);
|
||
|
|
return nothing{} if distance > reach + margin;
|
||
|
|
val normal := distance > tiny ? (c - nearest) * (1 / distance) : h.normals[face];
|
||
|
|
return one_point(normal, nearest, h.radius, c, radius, reach - distance);
|
||
|
|
}
|
||
|
|
|
||
|
|
# How far in front of a hull's edge another hull lies, of the edge that holds
|
||
|
|
# it furthest: the edge, and the distance, below 0 where they overlap.
|
||
|
|
struct apart {
|
||
|
|
var edge: u32;
|
||
|
|
var separation: f32;
|
||
|
|
}
|
||
|
|
|
||
|
|
def furthest_apart(from: &hull, other: &hull): apart {
|
||
|
|
var found := apart{ separation = -1e30f };
|
||
|
|
for i in 0..from.count {
|
||
|
|
val n := from.normals[i];
|
||
|
|
var least := 1e30f;
|
||
|
|
for j in 0..other.count {
|
||
|
|
least = math::min(least, n.dot(other.points[j] - from.points[i]));
|
||
|
|
}
|
||
|
|
if least > found.separation {
|
||
|
|
found = apart{ edge = i, separation = least };
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return found;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Two hulls of two or more points each.
|
||
|
|
def hulls(a: &hull, b: &hull, margin: f32): maybe<manifold> {
|
||
|
|
val reach := a.radius + b.radius;
|
||
|
|
val of_a := furthest_apart(a, b);
|
||
|
|
return nothing{} if of_a.separation > reach + margin;
|
||
|
|
val of_b := furthest_apart(b, a);
|
||
|
|
return nothing{} if of_b.separation > reach + margin;
|
||
|
|
# The face of the first is taken where it's as good, so the same pair gives
|
||
|
|
# the same face from one frame to the next.
|
||
|
|
val flip := of_b.separation > of_a.separation + 1e-4;
|
||
|
|
val reference := flip ? b : a;
|
||
|
|
val incident := flip ? a : b;
|
||
|
|
val face := flip ? of_b.edge : of_a.edge;
|
||
|
|
val separation := flip ? of_b.separation : of_a.separation;
|
||
|
|
|
||
|
|
if separation > 1e-4 && reach > 0 {
|
||
|
|
# Apart but for their round sides: they meet along the line between the
|
||
|
|
# hulls' nearest points, or along a face nearly that way.
|
||
|
|
var best := 1e30f;
|
||
|
|
var on_a := a.points[0];
|
||
|
|
var on_b := b.points[0];
|
||
|
|
for i in 0..edges_of(a) {
|
||
|
|
for j in 0..edges_of(b) {
|
||
|
|
val pair := nearest_of_segments(a.points[i], a.points[(i + 1) % a.count], b.points[j], b.points[(j + 1) % b.count]);
|
||
|
|
val d := (pair[1] - pair[0]).length_squared;
|
||
|
|
if d < best {
|
||
|
|
best = d;
|
||
|
|
on_a = pair[0];
|
||
|
|
on_b = pair[1];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val distance := math::sqrt(best);
|
||
|
|
return nothing{} if distance > reach + margin;
|
||
|
|
val normal := (on_b - on_a) * (1 / distance);
|
||
|
|
val along_face := flip ? normal * -1f : normal;
|
||
|
|
if along_face.dot(reference.normals[face]) < 0.995 {
|
||
|
|
return one_point(normal, on_a, a.radius, on_b, b.radius, reach - distance);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return clipped(reference, incident, face, flip, margin);
|
||
|
|
}
|
||
|
|
|
||
|
|
# How many distinct edges a hull has: a capsule's two are one segment.
|
||
|
|
def edges_of(h: &hull): u32 {
|
||
|
|
return h.count == 2 ? 1 : h.count;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The incident hull's edge facing most against the reference face, clipped to
|
||
|
|
# the face's width: each point of it within reach of the face a contact, named
|
||
|
|
# by the face, the edge, and which end of it the point is of.
|
||
|
|
def clipped(reference: &hull, incident: &hull, face: u32, flip: bool, margin: f32): maybe<manifold> {
|
||
|
|
val n := reference.normals[face];
|
||
|
|
val v1 := reference.points[face];
|
||
|
|
val v2 := reference.points[(face + 1) % reference.count];
|
||
|
|
var edge: u32 = 0;
|
||
|
|
var least := 1e30f;
|
||
|
|
for i in 0..incident.count {
|
||
|
|
val d := n.dot(incident.normals[i]);
|
||
|
|
if d < least {
|
||
|
|
least = d;
|
||
|
|
edge = i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var points: vec2[2] = [incident.points[edge], incident.points[(edge + 1) % incident.count]];
|
||
|
|
val tangent := (v2 - v1).normalized();
|
||
|
|
var kept: u32 = 2;
|
||
|
|
kept = clip(&mut points, kept, tangent * -1f, -tangent.dot(v1));
|
||
|
|
return nothing{} if kept < 2;
|
||
|
|
kept = clip(&mut points, kept, tangent, tangent.dot(v2));
|
||
|
|
return nothing{} if kept < 2;
|
||
|
|
|
||
|
|
val reach := reference.radius + incident.radius;
|
||
|
|
var made := manifold{ normal = flip ? n * -1f : n };
|
||
|
|
val flipped: u32 = flip ? 0x10000 : 0;
|
||
|
|
val named := flipped | (face << 8) | (edge << 1);
|
||
|
|
for i in 0..2u {
|
||
|
|
val s := n.dot(points[i] - v1);
|
||
|
|
continue if s > reach + margin;
|
||
|
|
# Midway between the reference's surface and the incident's.
|
||
|
|
val on_reference := points[i] - n * (s - reference.radius);
|
||
|
|
val on_incident := points[i] - n * incident.radius;
|
||
|
|
made.points[made.count] = contact{ point = (on_reference + on_incident) * 0.5, depth = reach - s, id = named | i as u32 };
|
||
|
|
made.count++;
|
||
|
|
}
|
||
|
|
return nothing{} if made.count == 0;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
# Clips a segment to the side of a line where `normal.dot(p) <= offset`, as
|
||
|
|
# Sutherland and Hodgman clip, keeping two points where any of it is there.
|
||
|
|
def clip(points: &mut vec2[2], kept: u32, normal: vec2, offset: f32): u32 {
|
||
|
|
val d0 := normal.dot(points[0]) - offset;
|
||
|
|
val d1 := normal.dot(points[1]) - offset;
|
||
|
|
return 2 if d0 <= 0 && d1 <= 0;
|
||
|
|
return 0 if d0 > 0 && d1 > 0;
|
||
|
|
val t := d0 / (d0 - d1);
|
||
|
|
val crossing := points[0] + (points[1] - points[0]) * t;
|
||
|
|
if d0 > 0 {
|
||
|
|
points[0] = crossing;
|
||
|
|
} else {
|
||
|
|
points[1] = crossing;
|
||
|
|
}
|
||
|
|
return 2;
|
||
|
|
}
|