Collision Detection
A generic collision manager built on the collide ecosystem. Manages collections of colliders with stable indices and provides multiple collision detection algorithms from O(n²) brute force to O(n log n) BVH.
Works in any dimension (2D, 3D, N-D) with any numeric type.
Quick Start
use CollisionManager;
use Sphere;
// Create a manager for sphere colliders, indexed by u32 IDs
let mut manager = new;
// Insert colliders with associated object IDs
let player_idx = manager.insert_collider;
let enemy_idx = manager.insert_collider;
// Update positions by replacing colliders
manager.replace_collider;
// Compute all collisions
let collisions = manager.compute_inner_collisions;
for in &collisions
Layers
There is no built-in layer/mask system. Instead, use separate CollisionManager instances as layers.
// Static level geometry — never collides with itself
let mut static_layer = new;
static_layer.insert_collider;
static_layer.insert_collider;
// Dynamic objects — need mutual collision
let mut dynamic_layer = new;
dynamic_layer.insert_collider;
dynamic_layer.insert_collider;
// Dynamic vs dynamic
let dynamic_collisions = dynamic_layer.compute_inner_collisions;
// Dynamic vs static — no compute_inner_collisions on static layer
let static_collisions = dynamic_layer.compute_collisions_with;
Each layer uses one collider type for maximum efficiency. For mixed shapes, use an enum:
Collision Algorithms
Every manager supports three algorithm variants. Pick the one that matches your trait bounds and performance needs:
| Method | Complexity | Required Traits |
|---|---|---|
compute_inner_collisions() |
O(n²) | Collider |
compute_inner_collisions_spatial() |
O(n×k) | Collider + SpatialPartition |
compute_inner_collisions_bvh::<V>() |
O(n log n) | Collider + Bounded<V> |
Same for compute_collisions_with / _spatial / _bvh.
Brute Force
Tests all pairs. No additional traits needed. Use for small scenes (<100 objects) or as a baseline.
Spatial Partitioning
Groups colliders into cells via SpatialPartition. Only tests pairs sharing a cell. Best for uniformly distributed objects of similar size.
BVH
Builds a bounding volume hierarchy via Bounded<V>. The type parameter V selects which bounding volume to use (e.g. Sphere for bounding spheres). Best for scenes with objects of varying sizes.
Broad-Phase Pre-Check
All algorithms call Collider::check_collision before the more expensive Collider::collision_info. Override check_collision on your collider with a cheap test (bounding sphere, AABB) to skip unnecessary narrow-phase GJK/EPA calls.
The Collide Ecosystem
| Crate | Description |
|---|---|
collide |
Core traits (Collider, Bounded, Transformable, SpatialPartition) |
collide-sphere |
Sphere collider — simplest and fastest |
collide-capsule |
Capsule collider — good for elongated shapes |
collide-convex |
Convex hull collider — arbitrary shapes via GJK/EPA |
collide-ray |
Ray type for intersection queries |
collision-detection |
This crate — collision manager |
Performance
- O(1) insert/remove by index
- O(n²) brute force, O(n×k) spatial partitioning, O(n log n) BVH
- Zero heap allocations after initial setup
This project is designed for use with AI coding assistants. Consistent API across all algorithm variants (same return type, same collision info structure), composable wrapper types (BoundedCollider, Transformed), and uniform naming conventions (compute_inner_collisions / _spatial / _bvh).