#! Contains `shape`: a sphere, a capsule, or a convex hull, about the origin of #! its body; the box holding it where a pose places it; whether it holds a #! place; and where a ray first meets it. import std::math; import malunal::geometry; import malunal::linear; module malunal::physics3d; #! What a shape is. pub enum shape_kind: u8 { #! The places within its radius of its middle. sphere, #! The places within its radius of a segment, between its two ends, as a #! pill. capsule, #! A convex polyhedron, as a box. hull, } #! A shape of a body, about the body's origin, as a pose places it: a sphere, #! a capsule, or a convex hull. A sphere is its middle and a radius around it, #! a capsule its two ends and a radius around the segment between them, and a #! hull its corners and faces. pub struct shape { var _kind: shape_kind; var _radius: f32; var _a: vec3; var _b: vec3; var _hull: hull; } #! Where a ray met a shape: how far along it, where, and which way the shape #! faces there. pub struct ray_hit { pub var distance: f32; pub var point: vec3; pub var normal: vec3; } #! A sphere of a radius, about a middle, the body's origin at first. pub def shape::sphere(radius: f32, center: vec3 = vec3::zero): shape { return shape{ _kind = shape_kind::sphere, _radius = radius, _a = center, _b = center }; } #! A capsule of a radius about the segment between two places: a sphere of the #! radius, where they're one place. pub def shape::capsule(a: vec3, b: vec3, radius: f32): shape { return shape::sphere(radius, a) if (b - a).length_squared <= tiny * tiny; return shape{ _kind = shape_kind::capsule, _radius = radius, _a = a, _b = b }; } #! A capsule standing up, along y, as a character's body: so wide and so high #! in all, its ends round, about a middle. Narrower than it's high, or a #! sphere. pub def shape::pill(width: f32, height: f32, center: vec3 = vec3::zero): shape { val radius := width / 2; val reach := math::max(height / 2 - radius, 0f); return shape::capsule(center - vec3{ 0, reach, 0 }, center + vec3{ 0, reach, 0 }, radius); } #! A box reaching so far each way from its middle, about a place, turned by a #! rotation. pub def shape::box(half: vec3, center: vec3 = vec3::zero, turn: quat = quat::identity): shape { return shape::of_hull(hull::box(half, center, turn)); } #! The convex hull around places, as `hull::of` makes it: nothing where they #! hold no volume, or their hull has too many corners. pub def shape::hull_of(places: span): maybe { if hull::of(places) is some(made) { return shape::of_hull(made); } return nothing{}; } #! A shape of a hull. pub def shape::of_hull(h: hull): shape { return shape{ _kind = shape_kind::hull, _a = h.center, _b = h.center, _hull = h }; } pub val shape::kind: shape_kind { get => this._kind; } #! How far around its middle or segment it reaches: a sphere's or a capsule's #! radius, and none of a hull. pub val shape::radius: f32 { get => this._radius; } #! A sphere's middle, or a capsule's first end. pub val shape::a: vec3 { get => this._a; } #! A capsule's second end, or a sphere's middle. pub val shape::b: vec3 { get => this._b; } #! A hull's corners and faces. pub def shape::as_hull(&this): &hull { return &this._hull; } #! The box holding it, where a pose places it. pub def shape::bounds(&this, at: pose): bounds { if this._kind == shape_kind::hull { val m := mat3::rotation(at.turn); var low := m * this._hull.point(0); var high := low; for i in 1..this._hull.count { val p := m * this._hull.point(i); low = low.min(p); high = high.max(p); } return bounds{ at.at + low, at.at + high }; } val a := at.apply(this._a); val b := at.apply(this._b); val r := vec3{ this._radius, this._radius, this._radius }; return bounds{ a.min(b) - r, a.max(b) + r }; } #! Whether it holds a place of space, where a pose places it, its surface #! among it. pub def shape::contains(&this, at: pose, p: vec3): bool { val local := at.unapply(p); if this._kind == shape_kind::hull { for f in 0..this._hull.faces { return false if this._hull.normal(f).dot(local) > this._hull.offset(f); } return true; } val nearest := nearest_on_segment(local, this._a, this._b); return (local - nearest).length_squared <= this._radius * this._radius; } #! Where a ray first meets it, where a pose places it: from a place, along a #! direction of length 1, as far as `reach`. A ray beginning within it meets #! it at 0, facing back along the ray. pub def shape::ray_cast(&this, at: pose, origin: vec3, direction: vec3, reach: f32): maybe { val o := at.unapply(origin); val d := at.unrotate(direction); var found: maybe = nothing{}; if this._kind == shape_kind::hull { found = ray_hull(&this._hull, o, d, reach); } else if this._kind == shape_kind::capsule { found = ray_capsule(o, d, reach, this._a, this._b, this._radius); } else { found = ray_sphere(o, d, reach, this._a, this._radius); } if found is some(hit) { return ray_hit{ distance = hit.distance, point = at.apply(hit.point), normal = at.rotate(hit.normal) }; } return nothing{}; } # A ray, of the shape's own places, meeting a sphere. def ray_sphere(o: vec3, d: vec3, reach: f32, center: vec3, radius: f32): maybe { val s := o - center; val c := s.dot(s) - radius * radius; return ray_hit{ distance = 0, point = o, normal = -d } if c <= 0; val b := s.dot(d); return nothing{} if b > 0; val disc := b * b - c; return nothing{} if disc < 0; val t := -b - math::sqrt(disc); return nothing{} if t > reach; val p := o + d * t; return ray_hit{ distance = t, point = p, normal = (p - center) * (1 / radius) }; } # A ray meeting a capsule: its round side, where it meets it between its ends, # as an infinite cylinder's, else its round ends. def ray_capsule(o: vec3, d: vec3, reach: f32, a: vec3, b: vec3, radius: f32): maybe { val nearest := nearest_on_segment(o, a, b); return ray_hit{ distance = 0, point = o, normal = -d } if (o - nearest).length_squared <= radius * radius; # The cylinder, as Ericson's Real-Time Collision Detection meets one: where # the ray's distance from its axis is the radius. val axis := b - a; val m := o - a; val dd := axis.dot(axis); val md := m.dot(axis); val nd := d.dot(axis); val qa := dd - nd * nd; if qa > tiny { val qb := dd * m.dot(d) - nd * md; val qc := dd * (m.dot(m) - radius * radius) - md * md; val disc := qb * qb - qa * qc; if disc >= 0 { val t := (-qb - math::sqrt(disc)) / qa; val along := md + t * nd; if t >= 0 && t <= reach && along >= 0 && along <= dd { val p := o + d * t; val on_axis := a + axis * (along / dd); return ray_hit{ distance = t, point = p, normal = (p - on_axis) * (1 / radius) }; } } } var best: maybe = nothing{}; val first := ray_sphere(o, d, reach, a, radius); val second := ray_sphere(o, d, reach, b, radius); if first is some(hit) { best = hit; } if second is some(hit) { if best is some(held) { if hit.distance < held.distance { best = hit; } } else { best = hit; } } return best; } # A ray meeting a hull, clipped by each face's side in turn, as Cyrus and Beck # clip a line: where it enters the last side it crosses into is where it meets # the hull. def ray_hull(h: &hull, o: vec3, d: vec3, reach: f32): maybe { var lower: f32 = 0; var upper := reach; var entered: i64 = -1; for f in 0..h.faces { val n := h.normal(f); val numerator := h.offset(f) - n.dot(o); val denominator := n.dot(d); if denominator == 0 { return nothing{} if numerator < 0; continue; } val t := numerator / denominator; if denominator < 0 && t > lower { lower = t; entered = f as i64; } else if denominator > 0 && t < upper { upper = t; } return nothing{} if upper < lower; } return ray_hit{ distance = 0, point = o, normal = -d } if entered < 0; return ray_hit{ distance = lower, point = o + d * lower, normal = h.normal(entered as u32) }; }