collision-detection 0.7.1

A generic collision detection library based on the `collide` crate
Documentation

Collision Detection

crates.io docs.rs pages

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 collision_detection::CollisionManager;
use collide_sphere::Sphere;

// Create a manager for sphere colliders, indexed by u32 IDs
let mut manager = CollisionManager::<Sphere<Vec3>, u32>::new();

// Insert colliders with associated object IDs
let player_idx = manager.insert_collider(Sphere::new(player_pos, 0.5), PLAYER_ID);
let enemy_idx = manager.insert_collider(Sphere::new(enemy_pos, 0.5), ENEMY_ID);

// Update positions by replacing colliders
manager.replace_collider(player_idx, Sphere::new(new_player_pos, 0.5));

// Compute all collisions
let collisions = manager.compute_inner_collisions();
for (collider_idx, hits) in &collisions {
    for hit in hits {
        println!("Object {} hit object {}", collider_idx, hit.index);
        let push_vector = hit.info.vector; // displacement to resolve collision
    }
}

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 = CollisionManager::<Sphere<Vec3>, u32>::new();
static_layer.insert_collider(wall_sphere, WALL_ID);
static_layer.insert_collider(floor_sphere, FLOOR_ID);

// Dynamic objects — need mutual collision
let mut dynamic_layer = CollisionManager::<Sphere<Vec3>, u32>::new();
dynamic_layer.insert_collider(player_sphere, PLAYER_ID);
dynamic_layer.insert_collider(enemy_sphere, ENEMY_ID);

// 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(&static_layer);

Each layer uses one collider type for maximum efficiency. For mixed shapes, use an enum:

enum Shape { Sphere(Sphere<Vec3>), Capsule(Capsule<Vec3>) }
impl Collider for Shape { /* dispatch to inner type */ }

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).