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
- ๐ฏ Project Overview
- โจ Key Features
- ๐๏ธ Architecture
- ๐ ๏ธ Tech Stack
- ๐ Getting Started
- ๐ฎ Controls
- ๐ Project Structure
- ๐ง How It Works
- ๐จ Customization
- ๐ค Contributing
- ๐ก Future Enhancements
- ๐ License
- ๐ Acknowledgments
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.
|
|
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
Why Vanilla JavaScript?
- ๐ฏ No build process or compilation
- ๐ฆ Zero npm dependencies
- ๐ Instant loading and execution
- ๐ Educational clarity and transparency
- A modern web browser (Chrome, Firefox, Edge, Safari)
- Basic understanding of HTML/JavaScript (for modifications)
1๏ธโฃ Clone the repository
git clone https://github.com/YOUR_USERNAME/Car_path_find_automation.git2๏ธโฃ Navigate to the project directory
cd Car_path_find_automation3๏ธโฃ 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.htmlOR just double-click index.html in your file explorer! ๐
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:8000Then navigate to http://localhost:8000 in your browser.
| 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 |
- 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
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
๐ 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 interpolationgetIntersection(): Line segment intersection detection- Used for sensor ray calculations
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
}The car uses ray-casting to detect its environment:
- 5 rays fan out from the car (90ยฐ spread)
- Each ray extends 150 pixels forward
- Rays check for intersections with road borders
- Shortest intersection distance is returned
- Yellow lines show active detection, black shows free space
// 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) * speedOpen 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 carOpen 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 visionModify colors in sensor drawing:
// sensor.js
ctx.strokeStyle = "yellow"; // Change sensor color
ctx.strokeStyle = "black"; // Change "dead zone" colorChange road styling in road.js:
ctx.strokeStyle = "white"; // Lane marking color
ctx.setLineDash([20, 20]); // Dashed line patternWe welcome contributions! Whether you're fixing bugs, improving documentation, or adding new features, your help is appreciated.
- ๐ Report bugs by opening an issue
- ๐ก Suggest new features
- ๐ Improve documentation
- ๐ง Submit pull requests
- Fork the repository
- Create a feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please read CONTRIBUTING.md for detailed guidelines.
- ๐ง 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
- 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
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
๐ 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
If you found this project helpful or interesting, please consider:
โญ Star this repository โข ๐ด Fork and experiment โข ๐ค Contribute improvements โข ๐ข Share with others
Made with โค๏ธ using Vanilla JavaScript