Skip to content

kartik912/Car_path_find_automation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

35 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿš— Self-Driving Car Path Finding Automation

License: MIT JavaScript HTML5 Canvas

An interactive self-driving car simulation built entirely with vanilla JavaScript, showcasing intelligent pathfinding and collision detection using ray-casting sensors.

๐ŸŽฎ Try Demo โ€ข ๐Ÿ“– Documentation โ€ข ๐Ÿค Contributing โ€ข ๐Ÿ“„ License


๐Ÿ“‹ Table of Contents


๐ŸŽฏ Project Overview

This project demonstrates a self-driving car simulation using intelligent pathfinding algorithms. Built from scratch with pure JavaScript and HTML5 Canvas, it features:

  • ๐Ÿš™ Real-time car physics with acceleration, friction, and steering mechanics
  • ๐Ÿ“ก Ray-casting sensor system for environment detection
  • ๐Ÿ›ฃ๏ธ Dynamic multi-lane road generation with infinite scrolling
  • ๐ŸŽฎ Manual control mode for testing and experimentation
  • ๐Ÿ–ฅ๏ธ Zero dependencies - runs directly in any modern browser

Perfect for: Learning game physics, understanding sensor-based navigation, and exploring autonomous vehicle concepts without complex frameworks.


โœจ Key Features

๐ŸŽฏ Core Functionality

  • โœ… Realistic Car Physics

    • Acceleration & deceleration
    • Friction simulation
    • Angular steering mechanics
    • Forward/reverse movement
  • โœ… Intelligent Sensors

    • 5-ray sensor array
    • 150-pixel detection range
    • Real-time collision detection
    • Visual sensor feedback

๐ŸŽจ Visual & Interactive

  • โœ… Dynamic Viewport

    • Camera follows the car
    • Smooth scrolling effect
    • Infinite road generation
    • 3-lane highway system
  • โœ… Interactive Controls

    • Arrow key steering
    • Responsive input handling
    • Real-time physics updates
    • Smooth animations (60 FPS)

๐Ÿ—๏ธ Architecture

graph TD
    A[index.html] --> B[main.js - Core Loop]
    B --> C[car.js - Vehicle Logic]
    B --> D[road.js - Road Generation]
    C --> E[controls.js - Input Handler]
    C --> F[sensor.js - Ray Casting]
    F --> G[utils.js - Math Helpers]
    D --> G
    
    style B fill:#4CAF50
    style C fill:#2196F3
    style F fill:#FF9800
Loading

๐Ÿ› ๏ธ Tech Stack

Technology Purpose Version
JavaScript Core Logic ES6+
HTML5 Structure 5
CSS3 Styling 3
Canvas Graphics HTML5

Why Vanilla JavaScript?

  • ๐ŸŽฏ No build process or compilation
  • ๐Ÿ“ฆ Zero npm dependencies
  • ๐Ÿš€ Instant loading and execution
  • ๐Ÿ“š Educational clarity and transparency

๐Ÿš€ Getting Started

Prerequisites

  • A modern web browser (Chrome, Firefox, Edge, Safari)
  • Basic understanding of HTML/JavaScript (for modifications)

Installation & Running

1๏ธโƒฃ Clone the repository

git clone https://github.com/YOUR_USERNAME/Car_path_find_automation.git

2๏ธโƒฃ Navigate to the project directory

cd Car_path_find_automation

3๏ธโƒฃ Launch the simulation

# Simply open index.html in your browser
# On Windows:
start index.html

# On macOS:
open index.html

# On Linux:
xdg-open index.html

OR just double-click index.html in your file explorer! ๐ŸŽ‰

Alternative: Local Server (Optional)

For the best experience, you can run it on a local server:

# Using Python 3
python -m http.server 8000

# Using Node.js (if you have http-server installed)
npx http-server

# Using PHP
php -S localhost:8000

Then navigate to http://localhost:8000 in your browser.


๐ŸŽฎ Controls

Key Action Description
โ†‘ Accelerate Increase forward speed
โ†“ Brake / Reverse Slow down or move backward
โ† Steer Left Turn the car left
โ†’ Steer Right Turn the car right

๐ŸŽฏ Pro Tips

  • Hold multiple keys for smooth diagonal movement
  • Use reverse gear to unstick from road borders
  • Watch the yellow sensor rays to understand detection
  • The car automatically slows down due to friction

๐Ÿ“ Project Structure

Car_path_find_automation/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ index.html           # Main HTML entry point
โ”œโ”€โ”€ ๐Ÿ“„ main.js              # Core animation loop & initialization
โ”œโ”€โ”€ ๐Ÿ“„ car.js               # Car class (physics & movement)
โ”œโ”€โ”€ ๐Ÿ“„ controls.js          # Keyboard input handler
โ”œโ”€โ”€ ๐Ÿ“„ sensor.js            # Ray-casting sensor system
โ”œโ”€โ”€ ๐Ÿ“„ road.js              # Road generation & rendering
โ”œโ”€โ”€ ๐Ÿ“„ utils.js             # Mathematical utility functions
โ”œโ”€โ”€ ๐Ÿ“„ style.css            # Minimal styling
โ”‚
โ”œโ”€โ”€ ๐Ÿ“– README.md            # This file
โ”œโ”€โ”€ ๐Ÿ“– CONTRIBUTING.md      # Contribution guidelines
โ””โ”€โ”€ ๐Ÿ“„ LICENSE              # MIT License

๐Ÿ“ฆ Module Breakdown

๐Ÿš— car.js - Vehicle Logic
  • Handles car position, speed, and angle
  • Implements physics (acceleration, friction, steering)
  • Integrates sensors and controls
  • Provides drawing methods
๐Ÿ“ก sensor.js - Ray-Casting System
  • Generates 5 sensor rays with 90ยฐ spread
  • Detects intersections with road borders
  • Returns distance to nearest obstacle
  • Renders sensor visualization
๐Ÿ›ฃ๏ธ road.js - Road Generation
  • Creates multi-lane highway
  • Defines road borders
  • Calculates lane center positions
  • Renders lane markings and borders
๐ŸŽฎ controls.js - Input Handler
  • Listens for arrow key events
  • Updates control state (forward, reverse, left, right)
  • Provides real-time input feedback
๐Ÿงฎ utils.js - Math Utilities
  • lerp(): Linear interpolation
  • getIntersection(): Line segment intersection detection
  • Used for sensor ray calculations

๏ฟฝ How It Works

๐Ÿšฆ The Main Loop

function animate() {
    car.update(road.borders);      // 1. Update car physics & sensors
    canvas.height = window.innerHeight; // 2. Clear canvas
    
    ctx.save();
    ctx.translate(0, -car.y + canvas.height * 0.7); // 3. Follow car
    
    road.draw(ctx);                // 4. Draw road
    car.draw(ctx);                 // 5. Draw car & sensors
    
    ctx.restore();
    requestAnimationFrame(animate); // 6. Loop at 60 FPS
}

๏ฟฝ Sensor System

The car uses ray-casting to detect its environment:

  1. 5 rays fan out from the car (90ยฐ spread)
  2. Each ray extends 150 pixels forward
  3. Rays check for intersections with road borders
  4. Shortest intersection distance is returned
  5. Yellow lines show active detection, black shows free space

๐ŸŽจ Physics Simulation

// Acceleration & Friction
speed += acceleration  // When moving forward
speed -= friction      // Always applied
speed = clamp(speed, -maxSpeed/2, maxSpeed)

// Steering (only when moving)
angle += turnRate * (speed > 0 ? 1 : -1)

// Position Update
x -= sin(angle) * speed
y -= cos(angle) * speed

๐ŸŽจ Customization

๐Ÿ”ง Easy Tweaks

Open main.js and modify:

// Change canvas width (road width)
canvas.width = 200;  // Try 300 for wider road

// Change car starting lane (0 = left, 1 = middle, 2 = right)
const car = new Car(road.getLaneCenter(1), 100, 30, 50);

Open car.js to adjust physics:

this.acceleration = 0.2;  // Increase for faster acceleration
this.maxSpeed = 4;        // Increase for higher top speed
this.friction = 0.05;     // Decrease for slipperier car

Open sensor.js to modify sensors:

this.rayCount = 5;        // More rays = better detection
this.rayLength = 150;     // Longer rays = earlier detection
this.raySpread = Math.PI/2; // Wider spread = peripheral vision

๐ŸŽจ Visual Customization

Modify colors in sensor drawing:

// sensor.js
ctx.strokeStyle = "yellow";  // Change sensor color
ctx.strokeStyle = "black";   // Change "dead zone" color

Change road styling in road.js:

ctx.strokeStyle = "white";   // Lane marking color
ctx.setLineDash([20, 20]);   // Dashed line pattern

๐Ÿค Contributing

We welcome contributions! Whether you're fixing bugs, improving documentation, or adding new features, your help is appreciated.

๐ŸŒŸ Ways to Contribute

  • ๐Ÿ› Report bugs by opening an issue
  • ๐Ÿ’ก Suggest new features
  • ๐Ÿ“ Improve documentation
  • ๐Ÿ”ง Submit pull requests

๐Ÿ“‹ Contribution Process

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please read CONTRIBUTING.md for detailed guidelines.


๐Ÿ’ก Future Enhancements

๐ŸŽฏ Planned Features

  • ๐Ÿง  Neural Network Integration - Add AI learning capabilities
  • ๐Ÿš— Traffic System - Multiple AI cars to avoid
  • ๐Ÿ† Scoring System - Track distance and performance
  • ๐Ÿ’พ Save/Load State - Persist progress in localStorage
  • ๐ŸŽจ Multiple Car Models - Different vehicle sprites
  • ๐Ÿ›ฃ๏ธ Complex Roads - Curves, intersections, obstacles
  • ๐Ÿ“Š Statistics Dashboard - Real-time performance metrics
  • ๐ŸŒˆ Themes - Day/night modes, different environments
  • ๐Ÿ”Š Sound Effects - Engine sounds and collision audio
  • ๐Ÿ“ฑ Mobile Support - Touch controls for mobile devices

๐Ÿš€ Advanced Ideas

  • Genetic Algorithm: Evolve better driving strategies
  • Reinforcement Learning: Train using Q-learning or PPO
  • Multiplayer Mode: Race against friends
  • Track Editor: Design custom road layouts
  • Replay System: Record and playback runs

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

MIT License - You are free to:
โœ… Use commercially
โœ… Modify
โœ… Distribute
โœ… Private use

๐Ÿ™ Acknowledgments

Special Thanks To

๐ŸŽ“ Educational Resources

๏ฟฝ Inspiration

  • Self-driving car research projects
  • Classic arcade racing games
  • Open-source game development community

๐Ÿ‘ฅ Community

  • All contributors who have helped improve this project
  • The open-source community for continuous inspiration
  • Hacktoberfest participants

โญ Show Your Support

If you found this project helpful or interesting, please consider:

Star on GitHub Fork on GitHub

โญ Star this repository โ€ข ๐Ÿด Fork and experiment โ€ข ๐Ÿค Contribute improvements โ€ข ๐Ÿ“ข Share with others


Made with โค๏ธ using Vanilla JavaScript

โฌ† Back to Top

Releases

No releases published

Packages

No packages published

Contributors 5