Self-driving car in Javascript

Inspiration YouTube

Radu course

Pipeline

Changelog

  • 2024-02-15 Add traffic
  • 2024-02-13 Collision detection
  • 2024-02-12 Polygon inner and outer track; Raycasting

Optizing code

for(let i=0; i<traffic.length; i++){
  traffic[i].update(road.borders);
}
traffic.forEach(vehicle => vehicle.update(road.borders));

for(let i=0; i<roadBorders.length; i++){
  if(polyIntersect(this.polygon, roadBorders[i])) {
  return true;
  }
}
if (roadBorders.some(border => polyIntersect(this.polygon, border)))
  return true;

#assesDamage(roadBorders, traffic){
  if(roadBorders.some(border => polyIntersect(this.polygon, border)))
  return true;

  if(traffic.some(car => polyIntersect(this.polygon, car.polygon)))
  return true;

  return false;
}
#assesDamage(roadBorders, traffic){
  return roadBorders.some(border => polyIntersect(this.polygon, border)) ||
  traffic.some(car => polyIntersect(this.polygon, car.polygon));
}