This project is a traffic simulation implemented in C using the SDL2 library. It simulates a four-way intersection with vehicles moving in different directions, traffic lights, and collision detection. The simulation is visualized using SDL2's rendering capabilities, providing a real-time view of traffic flow and vehicle interactions.
Before running the simulation, ensure you have the following installed:
- A C compiler (e.g.,
gcc
orclang
) - SDL2 library
- SDL2 development headers
- CMake (for building the project)
-
Install SDL2:
- On Arch Linux:
sudo pacman -S cmake sdl2
- On Windows:
- Download the SDL2 development libraries from the official SDL2 website.
- Follow the installation instructions provided.
- On Arch Linux:
-
Clone the Repository:
git clone https://github.com/razzat008/dsa-queue-simulator cd dsa-queue-simulator
-
Build the Project:
cd generator cmake -S . -B build/ cd build && make
-
Run the Simulation:
- To run the simulation:
./simulator
- To run the traffic generator:
./generator
- To run the simulation:
- SDL2: Used for rendering the simulation and handling window management.
- Standard C Libraries: Used for basic I/O, memory management, and data structures.
- CMake: Used for building the project.
The simulation runs in a window and displays vehicles moving through a four-way intersection. Traffic lights control the flow of vehicles, and collision detection ensures vehicles do not overlap.
- Close the Window: Click the close button or press
ESC
to exit the simulation. - Traffic Light Timing: Traffic lights switch automatically every 8.555 seconds.
The project is organized as follows:
dsa-queue-simulator/
├── generator/
│ ├── generator.c # Program for generating traffic by choosing a random lane
│ ├── CMakeLists.txt # CMake configuration for the generator
├── simulator/
│ ├── simulator.c # Main simulation program
│ ├── CMakeLists.txt # CMake configuration for the simulator
├── README.md # Project documentation
├── simulation.gif # Demo GIF of the simulation
generator.c
: Generates traffic by randomly assigning vehicles to lanes.simulator.c
: Simulates the traffic flow, including vehicle movement, traffic lights, and collision detection.CMakeLists.txt
: Configuration files for building the project using CMake.
- Vehicles are represented by the
Vehicle
struct, which includes properties such as position, speed, and target lane. - A
VehicleQueue
is used to manage vehicles waiting to enter the simulation, ensuring efficient enqueue and dequeue operations.
- Traffic lights are controlled by the
updateTrafficLights
function. - Lights switch between green and red states at regular intervals, simulating real-world traffic light behavior.
- SDL2 is used to render vehicles, roads, and traffic lights.
- The
DrawBackground
function draws the roads and lane markings. - The
drawVehicle
function renders each vehicle on the screen.
- Vehicles stop if the traffic light is red.
- Vehicles are prioritized based on traffic light states and lane rules.
- Vehicles in non-priority lanes stop when the traffic light is red.
- Official SDL2 Documentation: https://wiki.libsdl.org/
- SDL2 Tutorials: Lazy Foo' SDL Tutorials
- SDL2 GitHub Repository: https://github.com/libsdl-org/SDL
Below is a demo of the traffic simulation in action:
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Commit your changes and push them to your fork.
- Submit a pull request with a detailed description of your changes.
- Collision detection
- Error handling
- Dynamic Vehicle properties
- Bug: Traffic state changes when the vehicle hasnot passed thoroughly
This project is licensed under the MIT License. See the LICENSE file for details.