#collision #physics #geometry #game #vector

collide

Simple extensible collision management

3 unstable releases

Uses new Rust 2024

0.2.1 Mar 29, 2025
0.2.0 Jan 9, 2022
0.1.0 May 25, 2021

#576 in Algorithms

Download history 7/week @ 2024-12-14 2/week @ 2025-02-01 4/week @ 2025-02-08 3/week @ 2025-02-15 119/week @ 2025-03-29

119 downloads per month
Used in 3 crates

MIT/Apache

6KB

Collide

crates.io docs.rs

A generic trait system for collision detection in Rust. Implement collision checking between arbitrary shapes while maintaining dimension and scalar-type flexibility.

Features

  • 🧩 Generic Collider trait for cross-library interoperability
  • 📐 Dimension-agnostic through VectorSpace trait
  • 🚀 Supports 2D/3D/N-D collisions
  • 🔄 Bidirectional collision data with automatic perspective flipping

Basic Example

use collide::{Collider, CollisionInfo, VectorSpace};

struct Sphere<V: VectorSpace> {
    center: V,
    radius: V::Scalar,
}

impl<V: VectorSpace> Collider for Sphere<V> {
    type Vector = V;
    
    fn collision_info(&self, other: &Self) -> Option<CollisionInfo<V>> {
        let delta = other.center - self.center;
        let distance = delta.magnitude();
        let direction = delta / distance;
        let min_distance = self.radius + other.radius;
        
        if distance > min_distance {
            return None;
        }

        Some(CollisionInfo {
            self_contact: self.center + direction * self.radius,
            other_contact: other.center - direction * other.radius,
            vector: direction * (min_distance - distance),
        })
    }
}

Advanced Usage

Handle multiple collider types with an enum dispatch:

enum GenericCollider<V: VectorSpace> {
    Sphere { center: V, radius: V::Scalar },
    Box { center: V, size: V },
}

Design Goals

  1. Interoperability: Enable different physics engines to share collider implementations
  2. Flexibility: Support arbitrary dimensions (2D, 3D, ND) and scalar types (f32, f64)
  3. Extensibility: Add new collider types without breaking existing implementations

Dependencies

~155KB