-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.js
137 lines (106 loc) · 3.28 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class Sensor {
constructor(car) {
this.car = car;
this.rayCount = 5;
this.rayLength = 140;
this.raySpread = Math.PI / 2; // 45 deg (180deg / 4)
this.rays = [];
this.readings = [];
}
/**
* @param {{ x: number, y:number }[][]} roadBorders
* @param {any[]} traffic Array of Cars actually
*/
update(roadBorders, traffic) {
this.#castRays();
this.readings = [];
for (let i = 0; i < this.rays.length; i++) {
this.readings.push(this.#getReading(this.rays[i], roadBorders, traffic));
}
}
/**
* @param {[{ x: number, y:number }, { x: number, y:number }]} ray
* @param {[{ x: number, y:number }, { x: number, y:number }][]} roadBorders
* @param {any[]} traffic Array of Cars actually
*/
#getReading(ray, roadBorders, traffic) {
const touches = [];
for (let i = 0; i < roadBorders.length; i++) {
const border = roadBorders[i];
const touch = getIntersection(ray, border);
if (touch) {
touches.push(touch);
}
}
for (let i = 0; i < traffic.length; i++) {
const polygon = traffic[i].polygon;
for (let j = 0; j < polygon.length; j++) {
const point1 = polygon[j];
const point2 = polygon[(j + 1) % polygon.length];
const touch = getIntersection(ray, [point1, point2]);
if (touch) {
touches.push(touch);
}
}
}
if (touches.length === 0) {
return null;
}
const allOffsets = touches.map((item) => item.offset);
const minOffset = Math.min(...allOffsets);
return touches.find((item) => item.offset === minOffset);
}
#castRays() {
this.rays = [];
for (let i = 0; i < this.rayCount; i++) {
/**
* I think that `raySpread` is in a 360deg representation
* but we want to calculate in a unity circel format ([-180, 180])
* So we divide by 2 because the 360 to 180 maximum degree,
* and we pass on arg in negative format to represent the other extreme degree
*/
const rayAngle = lerp(
this.raySpread / 2,
-this.raySpread / 2,
this.rayCount === 1 ? 0.5 : i / (this.rayCount - 1)
);
// Se the sensor turn around with the car
const actualAngle = rayAngle + this.car.angle;
const start = {
x: this.car.x,
y: this.car.y,
};
const end = {
x: this.car.x - Math.sin(actualAngle) * this.rayLength,
y: this.car.y - Math.cos(actualAngle) * this.rayLength,
};
this.rays.push([start, end]);
}
}
/**
* @param {any} ctx A canvas 2d context
*/
draw(ctx) {
for (let i = 0; i < this.rayCount; i++) {
const ray = this.rays[i];
const rayStartPoint = ray[0];
let rayEndPoint = ray[1];
if (this.readings[i]) {
rayEndPoint = this.readings[i];
}
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "yellow";
ctx.moveTo(rayStartPoint.x, rayStartPoint.y);
ctx.lineTo(rayEndPoint.x, rayEndPoint.y);
ctx.stroke();
// The rest of the ray that would exists if there were nothing in front of it
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "black";
ctx.moveTo(ray[1].x, ray[1].y);
ctx.lineTo(rayEndPoint.x, rayEndPoint.y);
ctx.stroke();
}
}
}