A real-time traffic intersection simulation implemented in Rust using SDL2. The project simulates traffic flow at a four-way intersection with intelligent traffic light management and vehicle routing.
This simulation models a traffic intersection with the following key features:
- Two intersecting roads with one lane in each direction
- Traffic lights controlling vehicle flow
- Vehicles with different routing behaviors (left turn, right turn, straight)
- Intelligent traffic management system to prevent congestion
- Real-time user interaction for spawning vehicles
- Rust (latest stable version)
- SDL2 development libraries
sudo apt-get install libsdl2-devbrew install sdl2Download SDL2 development libraries from SDL2's website and set up according to their documentation.
- Clone the repository:
git clone https://github.com/yourusername/traffic-intersection
cd traffic-intersection- Build and run:
cargo run --release- ↑ Up Arrow: Spawn vehicle from South
- ↓ Down Arrow: Spawn vehicle from North
- → Right Arrow: Spawn vehicle from West
- ← Left Arrow: Spawn vehicle from East
- R: Spawn vehicle from random direction
- ESC: Exit simulation
- Fixed Velocity: All vehicles maintain a constant speed
- Safety Distance: Vehicles maintain a safe following distance
- Traffic Light Compliance: Vehicles stop at red lights and proceed on green
- No Route Changes: Vehicles must follow their predetermined route
- Congestion Control: System prevents traffic buildup (max 7 vehicles per lane)
The intersection uses a two-color (Red/Green) traffic light system positioned at each lane entry point. The traffic management algorithm prioritizes:
- Preventing collisions
- Minimizing congestion
- Optimizing flow through the intersection
.
├── src/
│ ├── main.rs # Application entry point
│ ├── map.rs # Intersection layout and rendering
│ ├── events.rs # Event handling
│ ├── cars/
│ │ ├── mod.rs # Vehicle module definitions
│ │ └── car.rs # Vehicle behavior implementation
│ └── entities/
│ ├── mod.rs # Game entity definitions
│ └── objects.rs # Core game objects
├── assets/ # Game assets (sprites, etc.)
├── Cargo.toml # Project dependencies
└── README.md # This file
This project is licensed under the MIT License - see the LICENSE file for details.