wreck
A 3D collision detection library for Rust. Built on top of glam for math and wide for SIMD acceleration.
Traits
Collides<T>- boolean collision test between two shapes. Every pair of built-in shapes has an implementation in both directions.Scalable- uniform scaling of a shape.Transformable- translate, rotate (viaMat3orQuat), or apply a fullAffine3transform.Stretchable- sweep a shape along a translation vector, producing the convex hull of the motion.Bounded- compute bounding volumes:broadphase()(bounding sphere),obb()(oriented bounding box), andaabb()(axis-aligned bounding box). Implemented for all volume types,ConvexPolygon,LineSegment,Pointcloud, andCollider. ForCollider, returns infinite bounds if the collider contains anyPlane,Line, orRay.
Shapes
Volumes
- Sphere - center + radius
- Capsule - line segment + radius, with a fast path for Z-aligned capsules
- Cuboid - oriented bounding box (center, 3 axes, half-extents), with fast paths for axis-aligned cases
- ConvexPolytope - arbitrary convex shape defined by half-planes and vertices, with an OBB broadphase
Surfaces
- Plane - infinite half-space defined by a normal and offset (
normal · x <= d) - ConvexPolygon - bounded convex polygon embedded in 3D, defined by a center, normal, and 2D vertices
Lines
- Line - infinite line through a point with a direction
- Ray - half-line with an origin and direction
- LineSegment - finite segment between two endpoints
Primitives
- Point - a single point in space
Spatial
- Pointcloud - point cloud backed by a CAPT spatial index for fast broadphase queries
All shape pairs implement Collides<T> for boolean intersection tests. The trait also provides collides_many which can be more performant.
Collider
Collider is a heterogeneous collection of shapes that can be tested against any single shape or another Collider. It supports Transformable, Scalable, applying the operation to every contained shape.
Stretchable is only implemented for Collider since there's no way to stretch a point cloud.
use Vec3;
use *;
let mut collider: Collider = Collider ;
// add shapes dynamically
collider.add;
// test against a single shape
let probe = new;
if collider.collides
Shapes API
Creating shapes
use Vec3;
use *;
// Sphere
let sphere = new;
// Capsule — defined by two endpoints and a radius
let capsule = new;
// Cuboid — oriented bounding box
let cuboid = new;
// or from an axis-aligned bounding box
let aabb = from_aabb;
// Point
let point = new;
// Line — infinite, through a point with a direction
let line = new;
let line = from_points;
// Ray — half-line from an origin
let ray = new;
// LineSegment — between two endpoints
let seg = new;
// Plane — infinite half-space (normal · x <= d)
let plane = new;
let plane = from_point_normal;
// ConvexPolygon — bounded polygon in 3D
let polygon = new;
// ConvexPolytope — arbitrary convex hull
let polytope = new;
Collision testing
use Collides;
// pairwise — works in both directions
sphere.collides;
capsule.collides;
// batch — SIMD-accelerated for some shapes
let others: = vec!;
sphere.collides_many;
Transforming shapes
use ;
let mut sphere = new;
// in-place
sphere.translate;
sphere.scale;
// or get a new copy
let moved = sphere.clone.translated;
let rotated = sphere.clone.rotated_quat;
let transformed = sphere.clone.transformed;
Bounding volumes
use Bounded;
// bounding sphere
let bsphere = capsule.broadphase;
// axis-aligned bounding box
let aabb = capsule.aabb;
// oriented bounding box
let obb = capsule.obb;
// works on Collider too — returns infinite bounds if it contains a Plane, Line, or Ray
let collider_aabb = collider.aabb;
Stretching (swept volumes)
use Stretchable;
// stretching a sphere along a direction produces a capsule
let capsule = new.stretch;
// stretching a cuboid produces either a larger cuboid (axis-aligned case)
// or a convex polytope (general case)
let swept = from_aabb
.stretch;
Features
wreck-assert— enables internal assertions in all buildsdebug-wreck-assert— enables internal assertions in debug builds only