-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessTriangle.js
More file actions
112 lines (88 loc) · 3.2 KB
/
ProcessTriangle.js
File metadata and controls
112 lines (88 loc) · 3.2 KB
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
function selectMoveDirection(listOfPoints, x, y, targetX, targetY, sideLength, translationMagnitude) {
var possibleDirections = 0; //LRTB
var test = [x, y - translationMagnitude];
if(!didCollideWithAnyPoint(test[0], test[1], listOfPoints,sideLength)) {
possibleDirections |= 1 << 1; //Possible Directions Includes Top
}
test = [x, y + translationMagnitude];
if(!didCollideWithAnyPoint(test[0], test[1], listOfPoints, sideLength)) {
possibleDirections |= 1 << 0; //Possible Directions Includes Bottom
}
test = [x - translationMagnitude, y];
if(!didCollideWithAnyPoint(test[0], test[1], listOfPoints, sideLength)) {
possibleDirections |= 1 << 3; //Possible Directions Includes Left
}
test = [x + translationMagnitude, y];
if(!didCollideWithAnyPoint(test[0], test[1], listOfPoints, sideLength)) {
possibleDirections |= 1 << 2; //Possible Directions Includes Right
}
var dx = targetX - x;
var dy = targetY - y;
if(possibleDirections == 0) {
return [0, 0]; //No Direction
}
var xMoveDirection = 0;
var yMoveDirection = 0;
var signX = dx == 0 ? 0 : Math.abs(dx)/dx; // if( dx == 0) 0 else abs(dx)/dx
var signY = dy == 0 ? 0 : Math.abs(dy)/ dy;
//Do Optimal Path
if(Math.abs(dx) > Math.abs(dy)) {
if(signX < 0 && (possibleDirections & (1 << 3)) > 0) {
xMoveDirection = signX; //left
} else if(signX > 0 && (possibleDirections & (1 << 2)) > 0) {
xMoveDirection = signX; //right
} else { //No Optimal X Component
if(signY < 0 && (possibleDirections & (1 << 1)) > 0) {
yMoveDirection = signY; //top
} else if(signY > 0 && (possibleDirections & (1 << 0)) > 0) {
yMoveDirection = signY; //bottom
}
}
} else {
if(signY < 0 && (possibleDirections & (1 << 1)) > 0) {
yMoveDirection = signY; //top
} else if(signY > 0 && (possibleDirections & (1 << 0)) > 0) {
yMoveDirection = signY; //bottom
} else { //No Optimal Y Component
if(signX < 0 && (possibleDirections & (1 << 3)) > 0) {
xMoveDirection = signX; //left
} else if(signX > 0 && (possibleDirections & (1 << 2)) > 0) {
xMoveDirection = signX; //right
}
}
}
//Do Secondary Path
if(xMoveDirection == 0 && yMoveDirection == 0) {
if((possibleDirections & (1 << 2)) > 0) {
xMoveDirection = 1;
} else if((possibleDirections & (1 << 1)) > 0) {
yMoveDirection = -1;
} else if((possibleDirections & (1 << 3)) > 0) {
xMoveDirection = -1;
} else if((possibleDirections & ( 1 << 0)) > 0) {
yMoveDirection = 1;
}
}
return [xMoveDirection, yMoveDirection];
}
function makeBody (x, y, r) {
return [x -r, x + r, y - r, y + r];
}
//Actually set triangle location to new location
function processTriangle(grid, triangle,v,dt) {
var triangleX = triangle.get_x();
var triangleY = triangle.get_y();
var allLocation = grid.getAllTrianglesAndSquares();
for (var i = 0; i < allLocation.length; i++){
if (allLocation[i].get_id() == triangle.get_id()){
allLocation.splice(i,1);
}
}
var circle = grid.getAllCircles()[0];
var circleX=circle.get_x();
var circleY=circle.get_y();
var MoveDirection = selectMoveDirection(allLocation, triangleX, triangleY, circleX, circleY, 40, v * dt);
triangleX += MoveDirection[0]*v*dt;
triangleY += MoveDirection[1]*v*dt;
triangle.move(triangleX,triangleY);
}