A realistic 4-way traffic intersection simulator built with Rust and SDL2. Features intelligent traffic light management, collision prevention, and multiple vehicle routing options.
- Multiple Routes: Vehicles can go straight, turn left, or turn right
- Color-Coded Routes:
- 🟡 Yellow: Left turns
- 🔵 Blue: Right turns
- ⚫ Grey: Straight through
- Realistic Movement: Vehicles follow proper turning paths through the intersection
- Safety Distance: Automatic collision prevention with 60-pixel following distance
- Lane Awareness: Vehicles stay in their designated lanes
- Sequential Direction Control: Each direction gets exclusive access to prevent all collisions
- Smart Phase Management:
- Phase 1: UP traffic only (from south)
- Phase 2: DOWN traffic only (from north)
- Phase 3: LEFT traffic only (from east)
- Phase 4: RIGHT traffic only (from west)
- Intersection Clearance: Extends red lights if vehicles are still clearing the intersection
| Key | Action |
|---|---|
↑ |
Spawn vehicle from South (going UP) |
↓ |
Spawn vehicle from North (going DOWN) |
← |
Spawn vehicle from East (going LEFT) |
→ |
Spawn vehicle from West (going RIGHT) |
R |
Spawn vehicle from random direction |
ESC |
Exit simulation |
- Rust (2021 edition or later)
- SDL2 development libraries
- SDL2_ttf for font rendering
sudo apt-get install libsdl2-dev libsdl2-ttf-devbrew install sdl2 sdl2_ttf- Download SDL2 development libraries from libsdl.org
- Follow the Rust-SDL2 installation guide
git clone [<repository-url>](https://github.com/achnitreda/road_intersection.git)
cd road_intersection
cargo runroad_intersection/
├── src/
│ └── main.rs # Main simulation code
├── Cargo.toml # Dependencies and project config
├── Cargo.lock # Dependency lock file
└── README.md # Readme
- Screen Size: 1000 x 800 pixels
- Intersection: 425-575 (X) × 325-475 (Y)
- Lane Width: ~75 pixels
- Vehicle Size: 50×50 pixels
| Direction | Position | Lane |
|---|---|---|
| UP (↑) | (515, 700) | Right lane |
| DOWN (↓) | (440, 0) | Left lane |
| LEFT (←) | (950, 335) | Top lane |
| RIGHT (→) | (10, 415) | Bottom lane |
- Frame Rate: 60 FPS
- Memory: Minimal - vehicles removed when off-screen
SDL2 Not Found
error: failed to run custom build command for `sdl2-sys`
Solution: Install SDL2 development libraries (see Installation section)
Font Loading Error
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value'
Solution: The program tries multiple font paths. Ensure you have system fonts installed.
Poor Performance
- Reduce vehicle count
- Check if running in debug mode (try
cargo run --release)
This project is open source. Feel free to use, modify, and distribute.
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
For questions, issues, or suggestions, please open an issue in the repository.
Built with ❤️ using Rust and SDL2