-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.js
330 lines (292 loc) · 8.02 KB
/
agent.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Constants
const kColor = 30;
const PI = Math.PI;
/**
* Represents the environment where the car drives
*/
class Arena {
constructor(width, height, canvas) {
this.width = width;
this.height = height;
this.canvas = document.createElement("canvas");
this.canvas.width = width;
this.canvas.height = height;
this.ctx = this.canvas.getContext("2d", { willReadFrequently: true });
this.ctx.fillStyle = "#000";
this.ctx.strokeStyle = "#000";
//Optimization
this.imageData = this.ctx.getImageData(0, 0, width, height);
this.buffer = new Uint32Array(this.imageData.data.buffer);
}
clearAll() {
this.ctx.clearRect(0, 0, this.width, this.height);
this.imageData = this.ctx.getImageData(0, 0, this.width, this.height);
this.buffer = new Uint32Array(this.imageData.data.buffer);
}
// Check if a point collides with any obstacle
collide(x, y) {
if (
typeof x !== "number" ||
typeof y !== "number" ||
isNaN(x) ||
isNaN(y) ||
x < 0 ||
y < 0 ||
x >= this.width ||
y >= this.height
) {
return false;
}
// Accès direct au buffer pour vérifier la collision
return this.buffer[Math.floor(y) * this.width + Math.floor(x)] !== 0;
}
// Calculate depth until collision along a ray
prof(x, y, theta, maxProf) {
const c = Math.cos(theta);
const s = Math.sin(theta);
let result = 0;
let currentX = x;
let currentY = y;
while (
currentX >= 0 &&
currentX < this.width &&
currentY >= 0 &&
currentY < this.height &&
!this.collide(Math.floor(currentX), Math.floor(currentY)) &&
result < maxProf
) {
currentX += c;
currentY += s;
result++;
}
return result;
}
// Generate random obstacles
generate(nbBoxes, boxHeight) {
this.clearAll();
// Draw border
this.ctx.strokeRect(0, 0, this.width - 1, this.height - 1);
// Generate random boxes
for (let i = 0; i < nbBoxes; i++) {
let x, y;
do {
x = Math.random() * (this.width - boxHeight);
y = Math.random() * (this.height - boxHeight);
} while (x < 40 && y < 40);
this.ctx.fillRect(x, y, boxHeight, boxHeight);
}
// Mettre à jour le buffer après avoir dessiné
this.imageData = this.ctx.getImageData(0, 0, this.width, this.height);
this.buffer = new Uint32Array(this.imageData.data.buffer);
}
}
/**
* Base class for the car that can drive in the arena
*/
class Agent {
constructor(
arena,
visionX,
visionY,
factorTheta,
factorDepth,
startSpeed,
vMin,
vMax,
frott,
) {
this.arena = arena;
this.visionX = visionX;
this.visionY = visionY;
this.factorTheta = factorTheta;
this.factorDepth = factorDepth;
this.vMin = vMin;
this.vMax = vMax;
this.frott = frott;
// Initialize state
this.init(startSpeed);
// Vision array
this.view = new Float32Array(visionX * visionY);
}
init(speed) {
this.x = 10;
this.y = 10;
this.v = speed;
this.theta = PI / 4;
this.vTheta = 0;
this.a = 0;
this.collision = 0;
this.distance = 0;
}
setAccel(acc) {
this.a = acc;
}
checkInside() {
return (
this.x > 0 &&
this.y > 0 &&
this.x < this.arena.width &&
this.y < this.arena.height
);
}
setRotation(rot) {
this.vTheta = Math.max(-PI / 3, Math.min(PI / 3, rot));
}
iterate(ctx, dt) {
const oldX = Math.floor(this.x);
const oldY = Math.floor(this.y);
// Update orientation
this.theta += this.vTheta * dt;
this.theta = this.theta < 0 ? this.theta + 2 * PI : this.theta;
this.theta = this.theta > 2 * PI ? this.theta - 2 * PI : this.theta;
// Update velocity
this.v += (this.a - this.frott * this.v) * dt;
this.v = Math.max(this.vMin, Math.min(this.vMax, this.v));
// Update position
this.x += this.v * Math.cos(this.theta) * dt;
this.y += this.v * Math.sin(this.theta) * dt;
// Check collision
this.collision = this.arena.collide(Math.floor(this.x), Math.floor(this.y));
if (this.collision) {
this.x = oldX;
this.y = oldY;
}
// Draw movement trail if context provided
if (ctx) {
ctx.beginPath();
ctx.moveTo(oldX, oldY);
ctx.lineTo(this.x, this.y);
ctx.strokeStyle = `rgb(${kColor + 64}, ${kColor + 64}, ${kColor + 64})`;
ctx.stroke();
}
// Update distance
this.distance +=
this.v * dt * (this.x / this.arena.width + this.y / this.arena.height);
}
vision(ctx) {
const step = (PI / 300) * this.factorTheta;
let tTheta = this.theta - (step * this.visionX) / 2;
for (let i = 0; i < this.visionX; i++, tTheta += step) {
const color =
i === Math.floor(this.visionX / 2) ? kColor - 3 : kColor + 1;
const dist = this.arena.prof(
this.x,
this.y,
tTheta,
this.visionY * this.factorDepth,
);
// Draw vision rays if context provided
if (ctx) {
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(
this.x + dist * Math.cos(tTheta),
this.y + dist * Math.sin(tTheta),
);
ctx.strokeStyle = `rgb(${color}, ${color}, ${color})`;
ctx.stroke();
}
// Update vision array
for (let j = 0; j < this.visionY; j++) {
this.view[i + j * this.visionX] = 0;
}
const ps = Math.floor(dist / this.factorDepth);
for (let j = ps; j < this.visionY; j++) {
this.view[i + j * this.visionX] = 1;
}
}
}
}
/**
* Extended car class with different vision method
*/
class MapAgent extends Agent {
constructor(
arena,
visionX,
visionY,
factorTheta,
factorDepth,
startSpeed,
vMin,
vMax,
frott,
) {
super(
arena,
visionX,
visionY,
factorTheta,
factorDepth,
startSpeed,
vMin,
vMax,
frott,
);
// Create viewer canvas
this.viewer = document.createElement("canvas");
this.viewer.width = visionX;
this.viewer.height = visionY;
this.viewerCtx = this.viewer.getContext("2d");
}
vision(ctx) {
const s = Math.sin(this.theta) * this.visionX * this.factorTheta;
const c = Math.cos(this.theta) * this.visionX * this.factorTheta;
// Calculate vision box corners
const Ax = Math.floor(this.x - s / 2);
const Ay = Math.floor(this.y + c / 2);
const Bx = Math.floor(this.x + s / 2);
const By = Math.floor(this.y - c / 2);
const Dx = Math.floor(
Ax + Math.cos(this.theta) * this.visionY * this.factorDepth - s,
);
const Dy = Math.floor(
Ay + Math.sin(this.theta) * this.visionY * this.factorDepth + c,
);
const Cx = Math.floor(
Bx + Math.cos(this.theta) * this.visionY * this.factorDepth + s,
);
const Cy = Math.floor(
By + Math.sin(this.theta) * this.visionY * this.factorDepth - c,
);
// Draw vision box if context provided
if (ctx) {
ctx.beginPath();
ctx.moveTo(Ax, Ay);
ctx.lineTo(Bx, By);
ctx.lineTo(Cx, Cy);
ctx.lineTo(Dx, Dy);
ctx.closePath();
ctx.strokeStyle = `rgb(${kColor + 32}, ${kColor + 32}, ${kColor + 32})`;
ctx.stroke();
}
// Transform view into vision array
// This is a simplified version as the original used complex mapping
this.viewerCtx.clearRect(0, 0, this.visionX, this.visionY);
this.viewerCtx.save();
this.viewerCtx.transform(
this.visionX / (Bx - Ax),
0,
0,
this.visionY / (Cy - By),
-Ax * (this.visionX / (Bx - Ax)),
-By * (this.visionY / (Cy - By)),
);
this.viewerCtx.drawImage(this.arena.canvas, 0, 0);
this.viewerCtx.restore();
// Update vision array
const imageData = this.viewerCtx.getImageData(
0,
0,
this.visionX,
this.visionY,
);
for (let i = 0; i < this.visionX; i++) {
for (let j = 0; j < this.visionY; j++) {
const idx = (i + j * this.visionX) * 4;
this.vision[i + j * this.visionX] =
imageData.data[idx + 3] === 0 ? 1 : 0;
}
}
}
}