A real-time traffic intersection simulation built with Rust and SDL2, featuring intelligent vehicle routing, collision detection, and traffic flow statistics.
Smart Road simulates a four-way intersection where vehicles approach from different directions and navigate through the intersection autonomously. The simulation tracks vehicle behavior, calculates crossing times, detects close calls, and provides comprehensive statistics on traffic flow.
Real-time traffic simulation showing vehicles navigating through the intersection
Comprehensive statistics tracking crossing times, velocities, and close calls
- Multi-directional Traffic: Vehicles approach from all four directions (North, South, East, West)
- Intelligent Routing: Vehicles can turn left, right, or go straight through the intersection
- Collision Detection: Real-time detection and prevention of vehicle collisions
- Statistics Tracking:
- Maximum and minimum crossing times
- Vehicle velocity monitoring
- Close call detection
- Total vehicle count
- Safe distance calculations
- Visual Simulation: Real-time graphical rendering with SDL2
- Interactive Controls: Keyboard-based vehicle spawning and simulation control
- Language: Rust (Edition 2021)
- Graphics: SDL2 with hardware acceleration
- Dependencies:
piston_window- Window managementsdl2- Graphics and rendering (with TTF and image support)lazy_static- Static initializationrand- Random vehicle generation
Before running the simulation, ensure you have:
- Rust (latest stable version)
- Cargo (comes with Rust)
- SDL2 development libraries
- On Ubuntu/Debian:
sudo apt-get install libsdl2-dev libsdl2-ttf-dev libsdl2-image-dev - On Fedora:
sudo dnf install SDL2-devel SDL2_ttf-devel SDL2_image-devel - On macOS:
brew install sdl2 sdl2_ttf sdl2_image - On Windows: Download SDL2 development libraries from libsdl.org
- On Ubuntu/Debian:
- Clone the repository:
git clone https://github.com/yourusername/smart-road.git
cd smart-road- Build the project:
cargo build --release- Run the simulation:
cargo run --release- Arrow Keys: Spawn vehicles from different directions
- ⬆️ Up Arrow: Spawn vehicle from South
- ⬇️ Down Arrow: Spawn vehicle from North
- ⬅️ Left Arrow: Spawn vehicle from East
- ➡️ Right Arrow: Spawn vehicle from West
- S: Display statistics window
- ESC: Exit simulation
smart-road/
├── src/
│ ├── main.rs # Entry point and main loop
│ ├── simulation.rs # Simulation logic and state management
│ ├── intersection.rs # Intersection management and collision detection
│ ├── vehicle.rs # Vehicle behavior and movement
│ ├── graphics.rs # Rendering utilities and constants
│ ├── statistics.rs # Statistics tracking and display
│ └── text_renderer.rs # Text rendering utilities
├── assets/
│ └── fonts/
│ └── Roboto-Regular.ttf
├── car.png # Vehicle sprite
├── Cargo.toml # Project dependencies
└── README.md
- Vehicle Spawning: Vehicles are spawned at the edges of the screen when arrow keys are pressed
- Route Selection: Each vehicle randomly selects a route (straight, left turn, or right turn)
- Movement: Vehicles move along predefined paths with velocity adjustments
- Collision Avoidance: The intersection monitors "hot places" to prevent collisions
- Statistics: The system tracks all vehicle movements and calculates performance metrics
- Rendering: SDL2 renders the intersection, roads, vehicles, and statistics in real-time
The simulation tracks and displays:
- Max Crossing Time: Longest time a vehicle took to cross the intersection
- Min Crossing Time: Shortest crossing time
- Max Velocity: Highest vehicle speed recorded
- Min Velocity: Lowest vehicle speed recorded
- Total Vehicles: Number of vehicles that have passed through
- Close Calls: Near-collision incidents detected
- Safe Distance: Minimum safe distance maintained between vehicles
Key simulation parameters can be adjusted in the source files:
WINDOW_WIDTHandWINDOW_HEIGHTingraphics.rsCAR_WIDTHandCAR_HEIGHTfor vehicle dimensionsMAX_VELOCITYinvehicle.rsfor speed limits- Frame rate (default: 60 FPS) in
main.rs
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
- Improve documentation
For questions or feedback, please open an issue on GitHub.
Note: This is a simulation project for educational purposes and traffic flow research.
⭐ Star this repository if you found it helpful! ⭐
Made with ❤️ from 🇸🇳