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
119 downloads per month
Used in 3 crates
6KB
Collide
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
- Interoperability: Enable different physics engines to share collider implementations
- Flexibility: Support arbitrary dimensions (2D, 3D, ND) and scalar types (f32, f64)
- Extensibility: Add new collider types without breaking existing implementations
Dependencies
~155KB