detour-crowd-rs is a pure Rust port of RecastNavigation's DetourCrowd module for steering thousands of autonomous agents across 3D navigation meshes. It combines spatial hash proximity grids, reciprocal velocity obstacles (RVO), and local path corridors to prevent agent crowding and deadlocks at 60+ FPS.
$ cargo add detour-crowd-rs
Watch 120+ autonomous agents navigate dense environments, resolving head-on deadlocks and bottleneck pinch points. Click anywhere to set a new global attractor target, or switch between pre-configured navigational scenarios.
A pure Rust port of the industry-standard navigation crowd system powering AAA game engines.
Implements Reciprocal Velocity Obstacles (RVO) where agents reciprocally share the avoidance responsibility. Uses adaptive multi-ring polar velocity space sampling matching Recast's dtObstacleAvoidanceQuery to test candidate velocities against dynamic agent circles and static segments.
Direct port of Recast's dtProximityGrid. Divides continuous 2D space into discrete cell buckets indexed by large prime hashes. Enables \(O(1)\) neighborhood lookup queries, completely avoiding slow \(O(N^2)\) pairwise distance checks across hundreds of crowd members.
Agents track local navigation corridors and corner sequences with smooth corner advancement. Automatically calculates optimal lookahead targets, prevents cutting corners into walls, and decelerates naturally upon approaching goal destinations.
Static line segments (walls, navmesh perimeter borders) apply repulsive steering forces that prevent agents from scraping against obstacles. In addition, soft separation forces prevent unnatural compression and interpenetration in dense bottleneck clusters.
Exact algorithms ported from Recast's DetourCrowd module.
The set of all relative velocities that will result in a collision between agent A and neighbor B within time horizon \(\tau\).
Each agent reciprocally takes half the required velocity change to avoid collision, eliminating mutual oscillatory indecision.
Samples polar candidate velocities and penalizes deviation from desired heading, time-to-impact (TOI), and incorrect passing side.
Maps continuous floating-point coordinates into discrete hash buckets using large primes \(p_1=73856093\), \(p_2=19349663\).
Create a crowd instance, set boundary segments, add agents, and update at 60 Hz.
[dependencies]
detour-crowd-rs = "0.1"
glam = "0.29"
use glam::Vec2;
use detour_crowd_rs::{Crowd, CrowdAgentParams};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Create crowd for up to 200 agents with 2.5m grid cells
let mut crowd = Crowd::new(200, 2.5);
// 2. Add static boundary walls (e.g. corridor or doorway)
crowd.add_boundary_segment(Vec2::new(-20.0, 2.0), Vec2::new(20.0, 2.0));
crowd.add_boundary_segment(Vec2::new(-20.0, -2.0), Vec2::new(20.0, -2.0));
// 3. Spawn opposing agents
let agent_a = crowd.add_agent(Vec2::new(-15.0, 0.0), CrowdAgentParams::default())?;
crowd.set_agent_target(agent_a, Vec2::new(15.0, 0.0))?;
let agent_b = crowd.add_agent(Vec2::new(15.0, 0.0), CrowdAgentParams::default())?;
crowd.set_agent_target(agent_b, Vec2::new(-15.0, 0.0))?;
// 4. Update simulation at 60 Hz
let dt = 1.0 / 60.0;
for _ in 0..300 {
crowd.update(dt);
}
if let Some(agent) = crowd.agent(agent_a) {
println!("Agent A arrived near: {:?}", agent.position);
}
Ok(())
}
let params = CrowdAgentParams {
radius: 0.5, // Agent body radius (m)
height: 2.0, // Physical height
max_acceleration: 8.0, // Acceleration limit (m/sยฒ)
max_speed: 3.5, // Desired walking speed (m/s)
collision_query_range: 8.0, // Neighbor check distance
separation_weight: 2.0, // Separation force factor
avoidance_profile: 0, // Quality tier index
};
Recast and Detour are traditionally C++ libraries with raw pointers and manual memory handling. detour-crowd-rs provides idiomatic, safe Rust APIs with zero unsafe blocks in crowd steering logic and full compile-time thread safety.
Comparing native Rust SIMD avoidance and spatial hashing against upstream RecastNavigation. View Full BENCHMARKS.md โ