Skip to content

Commit

Permalink
MAKE CAR SENSORS
Browse files Browse the repository at this point in the history
  • Loading branch information
THEKINGSTAR committed Dec 10, 2023
1 parent 62290e4 commit 8c20d25
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion main_project/car.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class Car{
this.friction=0.05;
this.angel=0;

this.controls=new Controls();
this.sensor = new Sensors(this);
this.controls = new Controls();
}

//Update the screen with the moving car object
update(){
this.#move();
this.sensor.update();
}

//Move method conation the car move by keys logic
Expand Down Expand Up @@ -92,5 +94,6 @@ class Car{
ctx.fill();

ctx.restore();
this.sensor.draw(ctx);
}
}
1 change: 1 addition & 0 deletions main_project/self_driving_car.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<body>
<canvas id="myCanvas"></canvas>
<script src="sensor.js"></script>
<script src="utils.js"></script>
<script src="road.js"></script>
<script src="controls.js"></script>
Expand Down
38 changes: 38 additions & 0 deletions main_project/sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@ class Sensors{
this.car = car;
this.rayCount=3;
this.rayLength=100;
this.raySpread=Math.PI/4;

this.rays=[];

}


update(){
this.rays == [];
for(let i = 0; i < this.rayCount; i++){
const rayAngle = lerp(
this.raySpread / 2,
-this.raySpread / 2,
i/(this.rayCount - 1)
);

const start = {x:this.car.x, y:this.car.y};
const end = {
x:this.car.x - Math.cos(rayAngle) * this.rayLength,
y: this.car.y = Math.cos(rayAngle) * this.rayLength
};
this.rays.push([start, end]);
}
}
draw(ctx){
for (let i = 0; i < array.rayCount; i++) {
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "yellow";
ctx.moveTo(
this.rays[i][0].x,
this.rays[i][0].y
);
ctx.lineTo(
this.rays[i][1].x,
this.rays[i][1].y
);
ctx.stroke();
}
}
}

0 comments on commit 8c20d25

Please sign in to comment.