-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.js
324 lines (286 loc) · 7.51 KB
/
car.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
const deg=0.0174533;
const maxDeflection = 30*deg;
const wheelBase = 2.55;
const speed = 1.0;
const h=0.1;
const carVertex = [
{x: 0.75, y:-0.88},
{x: 0.75, y: 0.88},
{x:-3.30, y: 0.88},
{x:-3.30, y:-0.88}];
let x=20;
let y=20;
let w=-90*deg;
let a=0;
let lastY=null;
let distance=0;
let car_enable=true;
// collect the information of all obstacles in the svg into an array
let obstacles=[];
Array.from(document.getElementById('obstacles').children).forEach(
function(rect) {
obstacles.push({
x0:rect.x.baseVal.value,
y0:rect.y.baseVal.value,
x1:rect.x.baseVal.value+rect.width.baseVal.value,
y1:rect.y.baseVal.value+rect.height.baseVal.value,
rect:rect});
});
// collect the information of all targets in the svg into an array
let targets=[];
Array.from(document.getElementById('targets').children).forEach(
function(rect) {
targets.push({
x0:rect.x.baseVal.value,
y0:rect.y.baseVal.value,
x1:rect.x.baseVal.value+rect.width.baseVal.value,
y1:rect.y.baseVal.value+rect.height.baseVal.value,
rect:rect});
});
let car=document.getElementById('car');
let frontwheel=document.getElementById('frontwheel');
function moveCar() {
if (!car_enable) {
return;
}
while (Math.abs(distance) >= h) {
if (distance > 0) {
x += h*Math.cos(w+a);
y += h*Math.sin(w+a);
w += Math.asin(Math.sin(a) * h / wheelBase);
distance -= h;
}
else {
w -= Math.asin(Math.sin(a) * h / wheelBase);
x -= h*Math.cos(w+a);
y -= h*Math.sin(w+a);
distance += h;
}
checkCollission();
}
car.setAttribute('transform', `translate(${x},${y})rotate(${w/deg})`);
frontwheel.setAttribute('transform', `rotate(${a/deg})`);
drawTrace();
}
moveCar();
function checkCollission(){
let x_min = x;
let x_max = x;
let y_min = y;
let y_max = y;
let cos = Math.cos(w);
let sin = Math.sin(w);
let u0 = x*cos + y * sin;
let v0 = -x*sin + y * cos;
let data = 'M ';
carVertex.forEach(function(v) {
let vx = x + v.x * cos - v.y * sin;
let vy = y + v.x * sin + v.y * cos;
data += `${vx},${vy} `;
if (vx < x_min) {
x_min = vx;
}
else if (vx > x_max) {
x_max = vx;
}
if (vy < y_min) {
y_min = vy;
}
else if (vy > y_max) {
y_max = vy;
}
});
data += 'Z';
//document.getElementById('debug').setAttribute('d', data);
//document.getElementById('debug2').setAttribute('d', '');
obstacles.forEach(function(obstacle) {
obstacle.rect.classList.remove("collide");
// if bounding box of car does not intersect, no further testing
if (obstacle.x1 < x_min || x_max < obstacle.x0) return;
if (obstacle.y1 < y_min || y_max < obstacle.y0) return;
let obVert = [
{x: obstacle.x0, y: obstacle.y0},
{x: obstacle.x1, y: obstacle.y0},
{x: obstacle.x1, y: obstacle.y1},
{x: obstacle.x0, y: obstacle.y1}];
let u_min = 1000000;
let u_max = -1000000;
let v_min = 1000000;
let v_max = -1000000;
data = 'M ';
obVert.forEach(function(v) {
let ox = -u0 + v.x * cos + v.y * sin;
let oy = -v0 - v.x * sin + v.y * cos;
data += `${ox},${oy} `;
if (ox < u_min) {
u_min = ox;
}
if (ox > u_max) {
u_max = ox;
}
if (oy < v_min) {
v_min = oy;
}
if (oy > v_max) {
v_max = oy;
}
});
data += 'Z';
//document.getElementById('debug2').setAttribute('d', data);
if (u_min > 0.75 || u_max < -3.30) return;
if (v_min > 0.88 || v_max < -0.88) return;
// the car collides with the obstacle
car_enable = false;
obstacle.rect.classList.add("collide");
document.getElementById('lose').setAttribute('style', 'display: block');
});
targets.forEach(function(target) {
target.rect.classList.remove("hit");
if (target.x0 <= x_min && x_max <= target.x1 &&
target.y0 <= y_min && y_max <= target.y1)
{
car_enable = false;
target.rect.classList.add('hit');
document.getElementById('win').setAttribute('style', 'display: block');
}
});
}
function drawTrace() {
let d1='0,0';
let d2='0,0';
let d3='0,0';
let d4='0,0';
let w=0;
let x=0;
let y=0;
let i,x1,x2,x3,x4,y1,y2,y3,y4;
let dw = Math.asin(Math.sin(a) * h / wheelBase);
for(i=0; i<60; i++) {
w -= dw;
x -= h*Math.cos(w+a);
y -= h*Math.sin(w+a);
let cos=Math.cos(w);
let sin=Math.sin(w);
x1 = x - 0.71*sin;
y1 = y + 0.71*cos;
x2 = x + 0.71*sin;
y2 = y - 0.71*cos;
x3 = x1 - 2.55*cos;
y3 = y1 - 2.55*sin;
x4 = x2 - 2.55*cos;
y4 = y2 - 2.55*sin;
d1 = `${x1},${y1} ` + d1;
d2 = `${x2},${y2} ` + d2;
d3 = `${x3},${y3} ` + d3;
d4 = `${x4},${y4} ` + d4;
}
d1='M' + d1;
d2='M' + d2;
d3='M' + d3;
d4='M' + d4;
x=0;
y=0;
w=0;
for(i=0; i<60; i++) {
x += h*Math.cos(w+a);
y += h*Math.sin(w+a);
w += dw;
let cos=Math.cos(w);
let sin=Math.sin(w);
x1 = x - 0.71*sin;
y1 = y + 0.71*cos;
x2 = x + 0.71*sin;
y2 = y - 0.71*cos;
x3 = x1 - 2.55*cos;
y3 = y1 - 2.55*sin;
x4 = x2 - 2.55*cos;
y4 = y2 - 2.55*sin;
d1 = d1 + ` ${x1},${y1}`;
d2 = d2 + ` ${x2},${y2}`;
d3 = d3 + ` ${x3},${y3}`;
d4 = d4 + ` ${x4},${y4}`;
}
document.getElementById('trace').setAttribute('d', d1+d2+d3+d4);
}
function handleMove(clientX, clientY, moveY) {
let client = document.documentElement.getBoundingClientRect();
let xRatio = (clientX - client.left) / client.width;
if (xRatio < 0.3) {
a = -maxDeflection;}
else if (xRatio < 0.5) {
a = -maxDeflection * (0.5 - xRatio) / 0.2;}
else if (xRatio <= 0.7) {
a = maxDeflection * (xRatio - 0.5) / 0.2;}
else {
a = maxDeflection;}
if (moveY) {
distance += 40*speed*(lastY - clientY)/client.height;
lastY = clientY;
}
moveCar();
}
function onMouseMove(evt) {
handleMove(evt.clientX, evt.clientY, (evt.buttons % 2 == 1));
}
function onMouseDown(evt) {
if (evt.buttons % 2 == 1) {
lastY = evt.clientY;
distance = 0;
}
}
function onKeyDown(evt) {
if (evt.key === 'ArrowUp' || evt.key === 'W' || evt.key === 'w'){
distance += 0.1;
moveCar();
}
else if (evt.key === 'ArrowDown' || evt.key === 'S' || evt.key === 's'){
distance -= 0.1;
moveCar();
}
}
function onWheel(evt) {
const BASE_SPEED = -6;
const FACTOR = [1, 1/40, 1/800];
evt.preventDefault();
distance += evt.deltaY * BASE_SPEED * FACTOR[evt.deltaMode];
moveCar();
}
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mousedown', onMouseDown);
document.addEventListener('keydown', onKeyDown);
document.addEventListener('wheel', onWheel);
function onTouchStart(evt) {
if (evt.touches.length === 1) {
evt.preventDefault();
}
}
function onTouchMove(evt) {
console.log(evt);
if (evt.touches.length === 1) {
evt.preventDefault();
handleMove(evt.touches[0].clientX, evt.touches[0].clientY, false);
}
}
function onTouchCancel(evt) {
if (evt.touches.length === 1) {
evt.preventDefault();
}
}
function onTouchEnd(evt) {
console.log(evt);
if (evt.changedTouches.length === 1) {
evt.preventDefault();
let client = document.documentElement.getBoundingClientRect();
if (evt.changedTouches[0].clientY < client.height / 2) {
distance = 5 * h;
}
else {
distance = -5 * h;
}
moveCar();
}
}
document.addEventListener('touchstart', onTouchStart);
document.addEventListener('touchmove', onTouchMove);
document.addEventListener('touchcancel', onTouchCancel);
document.addEventListener('touchend', onTouchEnd);