Skip to content

TanakAiko/smart-road

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Smart Road - Intersection Simulation

Rust SDL2 Cargo

A real-time traffic intersection simulation built with Rust and SDL2, featuring intelligent vehicle routing, collision detection, and traffic flow statistics.

🚗 Overview

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.

📸 Screenshots

Simulation in Action

Intersection Simulation Real-time traffic simulation showing vehicles navigating through the intersection

Statistics Dashboard

Statistics Comprehensive statistics tracking crossing times, velocities, and close calls

✨ Features

  • 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

🛠️ Technologies

  • Language: Rust (Edition 2021)
  • Graphics: SDL2 with hardware acceleration
  • Dependencies:
    • piston_window - Window management
    • sdl2 - Graphics and rendering (with TTF and image support)
    • lazy_static - Static initialization
    • rand - Random vehicle generation

📋 Prerequisites

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

🚀 Installation

  1. Clone the repository:
git clone https://github.com/yourusername/smart-road.git
cd smart-road
  1. Build the project:
cargo build --release
  1. Run the simulation:
cargo run --release

🎮 Controls

  • 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

📁 Project Structure

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

🎯 How It Works

  1. Vehicle Spawning: Vehicles are spawned at the edges of the screen when arrow keys are pressed
  2. Route Selection: Each vehicle randomly selects a route (straight, left turn, or right turn)
  3. Movement: Vehicles move along predefined paths with velocity adjustments
  4. Collision Avoidance: The intersection monitors "hot places" to prevent collisions
  5. Statistics: The system tracks all vehicle movements and calculates performance metrics
  6. Rendering: SDL2 renders the intersection, roads, vehicles, and statistics in real-time

📊 Statistics Metrics

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

🔧 Configuration

Key simulation parameters can be adjusted in the source files:

  • WINDOW_WIDTH and WINDOW_HEIGHT in graphics.rs
  • CAR_WIDTH and CAR_HEIGHT for vehicle dimensions
  • MAX_VELOCITY in vehicle.rs for speed limits
  • Frame rate (default: 60 FPS) in main.rs

🤝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest new features
  • Submit pull requests
  • Improve documentation

📧 Contact

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 🇸🇳

About

A real-time traffic intersection simulation built with Rust and SDL2, featuring intelligent vehicle routing, collision detection, and comprehensive traffic flow statistics.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages