-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensor.js
39 lines (35 loc) · 910 Bytes
/
sensor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Sensor {
constructor(car) {
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.sin(rayAngle) * this.rayLength,
y: this.car.y - Math.cos(rayAngle) * this.rayLength,
};
this.rays.push([start, end]);
}
}
draw(ctx) {
for (let i = 0; i < this.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();
}
}
}