-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
461 lines (379 loc) · 10.5 KB
/
index.html
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<!DOCTYPE html>
<html>
<body>
<div style="position: relative;">
<canvas id="layer1" width="1024" height="800" style="position: absolute; left: 0; top: 0; z-index: 0;"></canvas>
<canvas id="layer2" width="1024" height="800" style="position: absolute; left: 0; top: 0; z-index: 1;"></canvas>
</div>
<script>
var start;
var cleanM, clearQ;
var status = 0;
var target = 0;
function doAI(){
//USER CODE!
//be sure to have a starting point
if (start == undefined){
start = new Point(robot.pos.x, robot.pos.y);
}
if (bodyCollision){
if ( cleanM == undefined ){
var distFromStart = distance(start, robot.pos);
console.log("distFromStart "+distFromStart);
//be sure we travel far enought
if ( cleanM == undefined && distFromStart > robot.bodyRadius ){
cleanM = (start.y - robot.pos.y) / (start.x - robot.pos.x);
cleanQ = start.y - (cleanM * start.x);
console.log("found line to follow "+cleanM + " "+cleanQ);
status = 2;
}else{
robot.rotation = Math.random() * Math.PI * 2 - Math.PI; //-180..180 deg
console.log("collision too near, random rotation: "+robot.rotation);
}
}
if ( !isFinite(cleanM) ){ //cleanM is infinite: our base line was horizontal
//line is vertical
switch (+status){
case 1: //we was going up, go right
robot.rotation = Math.PI / 2; //turn right
target = robot.pos.x-robot.bodyRadius;
status = 3;
console.log("case 1 end target "+target);
break;
case 2: //we was going down, go left
target = robot.pos.x-robot.bodyRadius;
robot.rotation = -Math.PI / 2; //turn right
status = 4;
console.log("case 2 end target "+target);
break;
case 3: //we was going right, go down
robot.rotation = Math.PI / 2; //turn
status = 2;
console.log("case 3 special end");
break;
case 4: //we was going left, go up
robot.rotation = -Math.PI / 2; //turn right
status = 1;
console.log("case 4 special end");
break;
default:
console.log("unknow status -"+status+"-");
}
console.log("giong to rotate: "+robot.rotation + " "+status);
}else{
console.log("no implemented");
}
}
switch (+status){
case 3:
if (robot.pos.x < target){
robot.rotation = Math.PI / 2; //turn left
status = 2;
console.log("case 3 end");
}
break;
case 4:
if (robot.pos.x < target){
robot.rotation = -Math.PI / 2; //turn right
status = 1;
console.log("case 4 end");
}
break;
}
robot.move.x = Math.sin(robot.angle+robot.rotation)*(0.06);
robot.move.y = Math.cos(robot.angle+robot.rotation)*(0.06);
}
function Robot(x, y) {
this.x = x;
this.y = y;
this.radius = 1;
}
function RobotSensor() {
this.encoderLeft = 0;
this.encoderRight = 0;
this.bumpLeft = 0;
this.bumpRight = 0;
this.distanceFw = 0;
this.distanceBw = 0;
this.distanceLeft = 0;
this.distanceRight = 0;
}
function RobotAction() {
this.speed = 0;
this.rotation = 0;
}
function Point(x, y) {
this.x = x;
this.y = y;
}
var rooms = [
[
new Point(0,6),
new Point(6,6),
new Point(6,9),
new Point(4,9),
new Point(4,14),
new Point(0,14)
],
[
new Point(4,9),
new Point(10,9),
new Point(10,14),
new Point(4,14)
],
[
new Point(13,0),
new Point(20,0),
new Point(20,10),
new Point(13,10),
new Point(13,14),
new Point(10,14),
new Point(10,9),
new Point(6,9),
new Point(6,6),
new Point(13,6)
],
[
new Point(9,0),
new Point(13,0),
new Point(13,6),
new Point(9,6)
],
[
new Point(0,0),
new Point(9,0),
new Point(9,6),
new Point(0,6)
]
];
var startAngle = 0;
var robot = {
move:new Point(0,0),
rotation:0,
pos:new Point(12,12),
angle:startAngle,
bodyRadius:0.1,
radarRadius:0.3
};
var collisions = [];
reset();
window.setInterval(loop, 10);
function resetLayer1(){
var scale = 50;
var offsetX = 10;
var offsetY = 10;
//clear all, and draw the wall
var c = document.getElementById("layer1");
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
//draw the rooms
rooms.forEach(function(room, index, array) {
ctx.beginPath();
ctx.lineWidth="1";
ctx.strokeStyle="black"; // Wall
//jump to initial point
ctx.moveTo(room[0].x*scale+offsetX, room[0].y*scale+offsetY);
//draw each point. index 0 could be avoided at the beninginng since we are already there. Hope JS is smart and does not fuck up
room.forEach(function(item, index, array) {
ctx.lineTo(item.x*scale+offsetX,item.y*scale+offsetY);
});
//close the figure
ctx.lineTo(room[0].x*scale+offsetX, room[0].y*scale+offsetY);
ctx.stroke(); // Draw it
});
}
function drawLayer1(){
var scale = 50;
var offsetX = 10;
var offsetY = 10;
var c = document.getElementById("layer1");
var ctx = c.getContext("2d");
//draw the robot path
ctx.beginPath();
ctx.fillStyle = "rgba(255, 0, 0, 0.2)";
ctx.arc(robot.pos.x*scale+offsetX,robot.pos.y*scale+offsetY,robot.bodyRadius*scale,0,2*Math.PI);
ctx.fill();
}
function drawLayer2(){
var scale = 50;
var offsetX = 10;
var offsetY = 10;
var c = document.getElementById("layer2");
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
//draw a line that show the "head" of the bot
ctx.beginPath();
ctx.strokeStyle = "Blue";
ctx.lineWidth=0.1*scale;
ctx.moveTo(robot.pos.x*scale+offsetX,robot.pos.y*scale+offsetY);
var hX = (robot.pos.x + Math.sin(robot.angle)*robot.bodyRadius) *scale+offsetY;
var hY = (robot.pos.y + Math.cos(robot.angle)*robot.bodyRadius) *scale+offsetY;
ctx.lineTo(hX, hY);
ctx.stroke();
//draw the radar
ctx.beginPath();
ctx.strokeStyle = "Blue";
ctx.lineWidth=0.01 * scale;
ctx.arc(robot.pos.x*scale+offsetX,robot.pos.y*scale+offsetY,robot.radarRadius*scale,0,2*Math.PI);
ctx.stroke();
collisions.forEach(function(collision, index, array) {
//draw the collision
ctx.beginPath();
ctx.fillStyle = "Green";
ctx.lineWidth="1";
ctx.arc(collision.x*scale+offsetX,collision.y*scale+offsetY,2,0,2*Math.PI);
ctx.fill();
});
}
function reset(){
resetLayer1();
robot = {move:new Point(0,0),rotation:0,pos:new Point(12,12), angle:startAngle, bodyRadius:0.1, radarRadius:0.3};
var bodyCollision = false;
collisions = [];
}
//loop();
function loop(){
//console.log("loop");
robot.pos.x+=robot.move.x;
robot.pos.y+=robot.move.y;
robot.angle+=robot.rotation;
bodyCollision = false;
collisions = [];
findCollision(robot)
drawLayer1();
drawLayer2();
robot.move.x = robot.move.y = robot.rotation = 0;
doAI();
}
function findCollision(robot){
rooms.forEach(function(room, index, array) {
//console.log("ROOM "+index);
var i;
for (i = 0; i < room.length-1; i++){
p = findTangentPoint(robot.pos.x, robot.pos.y, room[i].x, room[i].y, room[i+1].x, room[i+1].y);
//console.log("Tangent point is " +p.x+","+p.y);
collide(robot, p);
}
p = findTangentPoint(robot.pos.x, robot.pos.y, room[i].x, room[i].y, room[0].x, room[0].y);
collide(robot, p);
});
}
function distance(a, b){
return Math.sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
function collide(robot, point){
var rangeBody = 2, rangeRadar = 5;
var dist = distance(robot.pos, point);
//console.log("distance " +dist);
if (dist <= robot.radarRadius ){
//i see you
//console.log("i see something");
//collisions.push(point);
if (dist <= robot.bodyRadius ){
//i touch you
bodyCollision = true;
console.log("i touch something");
collisions.push(point);
if (dist < robot.bodyRadius ){
//i'm inside the wall! take me out of there!
//we just move back the robot
robot.pos.x -= robot.move.x;
robot.pos.y -= robot.move.y;
//ALTERNATIVE2 : just dont move
robot.move.x = 0;
robot.move.y = 0;
//ALTERNAIVE1: try to scale back movement vector
/*
var distanceMoved = distance( robot.move, new Point(0,0) );
var distancePossibleToMove = distanceMoved - (robot.bodyRadius - dist);
distancePossibleToMove = Math.abs(distancePossibleToMove);
robot.move.x = (robot.move.x/distanceMoved)*distancePossibleToMove;
robot.move.y = (robot.move.y/distanceMoved)*distancePossibleToMove;
robot.pos.x += robot.move.x;
robot.pos.y += robot.move.y;
*/
//now i HOPE it does not collide animore. May NOT be tha case in case of corner; in that case i don't know what to do. sorry
}
}
}
}
function constrain(val, min, max){
if (min > max){
return constrain(val, max, min);
}
if (val < min)
return min;
if (val > max)
return max;
return val;
}
function findTangentPoint(xR, yR, xW1, yW1, xW2, yW2 ){
if ( xW1 == xW2 ){
//console.log("same x " +yR +" "+ yW1+" "+ yW2);
return new Point(xW1, constrain(yR, yW1, yW2) );
}
if ( yW1 == yW2 ){
//console.log("same y " +xR +" "+ xW1+" "+ xW2);
return new Point(constrain(xR, xW1, xW2), yW1);
}
mW = (yW1 - yW2) / (-xW1 + xW2);
qW = -mW * xW1 + yW1;
console.log("Equation of the wall is y=" +mW+"x+"+qW);
if ( isFinite(mW) ){
mI = 0;
}
//first we find the m and q of the line tangent to the wall passing by the center of the robot
var mI = -1/mW;
var qI = mI*xR-yR
console.log("tangent line " +mI+" "+qI);
//now we find the point where the tangent line and the wall line intersect
var x = (-qW+qI)/(mW-mI);
var y = mW * x + qW;
//DEBUG: doublecheck
var yDebug = mI * x + qI;
if (yDebug != y){
console.log("ERROR: tangent point is wrong! " +x+" "+y+" "+yDebug);
}
return new Point(x,y);
}
function cleanRoom(room){
// now we try to be smartass
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
//get the first wall and make it into a line
//y = mx + q
var x1 = room[0].x;
var x2 = room[1].x;
var y1 = room[0].y;
var y2 = room[1].y;
console.log("x1 "+x1+" y1 "+y1);
console.log("x2 "+x2+" y2 "+y2);
var correctionM = 0;
var correctionQ = 0;
if (x1 != x2){
var deltaX = x1-x2;
var deltaY = y1-y2;
var m = deltaY / deltaX;
correctionM = m;
}
correctionQ = y1 - x1 * correctionM;
console.log("correctionM "+correctionM+" correctionQ "+correctionQ);
//need to find higest Y
for (var i = 0; i > -6; i--){
ctx.beginPath();
ctx.lineWidth="4";
ctx.strokeStyle="red"; // path
var targetX1 = x1;
var targetY1 = correctionM * x1 + correctionQ + i;
console.log("tx1 "+targetX1+" ty1 "+targetY1);
ctx.moveTo(targetX1 * scale+offsetX, targetY1 * scale+offsetY);
var targetX2 = x2;
var targetY2 = correctionM * x2 + correctionQ + i;
console.log("tx2 "+targetX2+" ty2 "+targetY2);
ctx.lineTo(targetX2 * scale+offsetX, targetY2 * scale+offsetY);
ctx.stroke(); // Draw it
}
}
</script>
</body>
</html>