A real-time autonomous vehicle traffic simulation built in Rust using SDL2 that implements intelligent intersection management without traffic lights.
This project simulates a smart intersection where autonomous vehicles approach from four directions and can turn left, right, or go straight. The system uses a grid-based time-space reservation algorithm to prevent collisions and optimize traffic flow without traditional traffic control mechanisms.
- Collision-free intersection management using time-space cell reservations
- Real-time vehicle physics with three distinct velocity levels (Slow/Medium/Fast/Stopped)
- Dynamic speed adaptation based on traffic conditions and intersection permissions
- Safety distance enforcement between vehicles to prevent collisions
- Animated vehicle movement with proper rotation during turns
- Comprehensive statistics tracking for performance analysis
- Route adherence: Vehicles follow predetermined lanes (left turn, straight, right turn)
- Adaptive speed control: Automatic velocity adjustment based on traffic and intersection status
- Turn animation: Vehicles rotate correctly when changing direction
- Safety distance: Maintains configurable following distances
- Grid-based reservation system: 10x10 pixel cells with time-slot booking
- Two-path collision detection: Separate handling for straight and turning vehicles
- Dynamic permission system: Real-time intersection access control
- Close call detection: Monitors safety violations between vehicles
src/
├── main.rs # Game loop, SDL2 initialization, input handling
├── intersection.rs # Smart intersection management and collision prevention
├── vehicle.rs # Vehicle physics, movement, and collision detection
├── route.rs # Direction and route positioning logic
├── stats.rs # Statistics display with animated background
└── velocities.rs # Speed enumeration definitions
- Rust (latest stable version)
- SDL2 development libraries
- SDL2_image library
- SDL2_ttf library (for statistics display)
Ubuntu/Debian:
sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-ttf-devmacOS (Homebrew):
brew install sdl2 sdl2_image sdl2_ttfWindows: Download SDL2, SDL2_image, and SDL2_ttf development libraries from libsdl.org
Create this directory structure:
assets/
├── road-intersection/
│ └── road-intersection.png # Intersection background image
├── Cars/
│ ├── car1.png # Vehicle sprites (40x70 pixels)
│ ├── car2.png
│ ├── car3.png
│ ├── car4.png
│ └── car5.png
└── fonts/
└── Orbitron-VariableFont_wght.ttf # Font for statistics display
git clone https://github.com/moseeh/smart-road
cd smart-road
cargo run- Arrow Keys: Spawn vehicles from specific directions
- Up Arrow: Generate vehicle from south to north
- Down Arrow: Generate vehicle from north to south
- Right Arrow: Generate vehicle from west to east
- Left Arrow: Generate vehicle from east to west
- R: Continuously generate random vehicles
- S: Stop continuously spawninng random vehicles
- ESC: Exit simulation and display statistics
- Vehicles spawn with random routes (left/straight/right)
- Anti-spam protection prevents vehicles from spawning on top of each other
- Each vehicle gets a unique ID and texture variant
- Canvas dimensions: 1000x1000 pixels
- Intersection zone: 350-650 pixels (300x300 square)
- Vehicle size: 40x70 pixels
- Grid resolution: 10x10 pixel cells for collision detection
- Velocity system: 3.0, 5.0, 7.0 pixels/frame (180, 300, 420 pixels/second at 60 FPS)
- Time calculation: Based on distance/velocity with frame rate conversion
- Safety distance: Configurable following distance (default: 50 pixels + vehicle length)
- Turn mechanics: Vehicles execute turns at predetermined coordinates with rotation
Each direction has three dedicated lanes:
- North: x-coordinates 500 (left), 550 (straight), 600 (right)
- South: x-coordinates 450 (left), 400 (straight), 350 (right)
- East: y-coordinates 500 (left), 550 (straight), 600 (right)
- West: y-coordinates 450 (left), 400 (straight), 350 (right)
- Path calculation: Pre-computed cell sequences for each direction/route combination
- Time slot booking: Vehicles reserve cells for specific time intervals
- Conflict detection: Prevents overlapping reservations in same cells
- Dynamic speed adjustment: Reduces speed when conflicts detected
- Progressive release: Cells released as vehicles pass through them
- Spatial separation: Grid-based cell reservation prevents same-space conflicts
- Temporal coordination: Time-based bookings prevent timing conflicts
- Safety buffers: Following distance requirements prevent rear-end collisions
- Turn coordination: Special handling for vehicles changing direction
The system monitors and reports:
- Total vehicles passed: Count of vehicles completing intersection traversal
- Velocity statistics: Maximum and minimum speeds recorded across all vehicles
- Intersection timing: Maximum and minimum time spent in intersection area
- Close calls: Safety distance violations between vehicles
- Active vehicle count: Real-time count of vehicles in simulation
Statistics display features:
- Animated car background during statistics screen
- Color-coded text (white labels, yellow values, cyan highlights)
- Orbitron font for futuristic appearance
- Precise label alignment for professional presentation
- Frame rate: Locked at 60 FPS with VSync
- Memory efficiency: Reusable vehicle textures, efficient grid storage
- Computational complexity: O(n) vehicle updates, O(1) cell access
- Scalability: Configurable grid resolution for performance tuning
- No wait times: Vehicles proceed when intersection is clear
- Dynamic adaptation: Real-time response to traffic patterns
- Higher throughput: No fixed timing constraints
- Perfect coordination: No human error or reaction delays
- Optimal spacing: Precise safety distance maintenance
- Predictable behavior: Deterministic movement patterns
cargo build --release # Optimized build
cargo run # Development run
cargo test # Run tests (if implemented)- Modular design: Separate concerns across multiple files
- Type safety: Strong typing with Rust's ownership system
- Error handling: Proper Result types for fallible operations
- Performance: Zero-cost abstractions and efficient algorithms
- Single intersection: Only handles one intersection type
- Fixed lanes: No lane changing or route deviation
- Deterministic spawning: Limited randomization in vehicle generation
- Static assets: Requires pre-loaded image and font files
- Multiple intersection types (T-junctions, roundabouts)
- Variable speed limits and acceleration/deceleration physics
- Emergency vehicle prioritization
- Network of connected intersections
- Machine learning optimization
- Real-world data integration
This simulation demonstrates practical applications of autonomous vehicle coordination algorithms and provides a foundation for advanced traffic management system research.