-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.js
178 lines (155 loc) · 4.18 KB
/
physics.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
var DIRS = {
W : "west",
N : "north",
E : "east",
S : "south"
};
var DIR = {
W : new Direction(DIRS.W),
N : new Direction(DIRS.N),
E : new Direction(DIRS.E),
S : new Direction(DIRS.S)
};
function Direction(direction) {
this.direction = direction;
this.opposite = function() {
if (this.direction == DIRS.W) {
return new Direction(DIRS.E);
}
if (this.direction == DIRS.E) {
return new Direction(DIRS.W);
}
if (this.direction == DIRS.N) {
return new Direction(DIRS.S);
}
if (this.direction == DIRS.S) {
return new Direction(DIRS.N);
}
};
Direction.prototype.equals = function(other) {
return other.direction === this.direction;
};
Direction.prototype.isVertical = function() {
return this.direction == DIRS.N || this.direction == DIRS.S;
};
}
function Rectangle(x, y, w, h) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
Rectangle.prototype.getEdge = function(dir) {
if (dir.equals(DIR.W)) {
return new Edge(this.x, this.y, this.x, this.y + this.h);
} else if (dir.equals(DIR.N)) {
return new Edge(this.x, this.y, this.x + this.w, this.y);
} else if (dir.equals(DIR.E)) {
return new Edge(this.x + this.w, this.y, this.x + this.w, this.y
+ this.h);
} else if (dir.equals(DIR.S)) {
return new Edge(this.x, this.y + this.h, this.x + this.w, this.y
+ this.h);
}
};
}
Rectangle.prototype.getX = function() {
return this.x;
};
Rectangle.prototype.getY = function() {
return this.y;
};
Rectangle.prototype.getH = function() {
return this.h;
};
Rectangle.prototype.getW = function() {
return this.w;
};
function Edge(x1, y1, x2, y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
Edge.prototype.isVerticalEdge = function() {
return this.x1 === this.x2;
};
}
// Throws error in case rectangles never collide
function timeToCollision(movingRect, vector, stillRect) {
var yTime = calculateYTimeToCollision(movingRect, vector, stillRect);
var xTime = calculateXTimeToCollision(movingRect, vector, stillRect);
var res = calculateCollisionTimeAndDirection(vector, yTime, xTime);
if (res.direction === undefined) {
throw "could not determine collision direction";
}
if (res.time === Infinity) {
throw "Infinity until collision";
}
return res;
}
function calculateCollisionTimeAndDirection(vector, yTime, xTime) {
var res = {};
if (yTime < xTime) {
res.time = yTime;
res.direction = vector.getCardinalDirections().h;
} else {
res.time = xTime;
res.direction = vector.getCardinalDirections().v;
}
return res;
}
function calculateXTimeToCollision(movingRect, vector, stillRect) {
var movingCollisionDirs = vector.getCardinalDirections();
if (movingCollisionDirs.v === undefined) {
return Infinity;
}
var movingEdge = movingRect.getEdge(movingCollisionDirs.v);
var stillEdge = stillRect.getEdge(movingCollisionDirs.v.opposite());
var xDistance = stillEdge.x1 - movingEdge.x1;
var xTime = xDistance / vector.vx;
if (xTime < 0) {
xTime = Infinity;
}
return xTime;
}
function calculateYTimeToCollision(movingRect, vector, stillRect,
movingCollisionDirs) {
var movingCollisionDirs = vector.getCardinalDirections();
if (movingCollisionDirs.h === undefined) {
return Infinity;
}
var movingEdge = movingRect.getEdge(movingCollisionDirs.h);
var stillEdge = stillRect.getEdge(movingCollisionDirs.h.opposite());
var yDistance = stillEdge.y1 - movingEdge.y1;
var yTime = yDistance / vector.vy;
if (yTime < 0) {
yTime = Infinity;
}
return yTime;
}
// ----------------------------------------------------------------------------------------
function Vector(vx, vy) {
this.vx = vx;
this.vy = vy;
}
Vector.prototype.getCardinalDirections = function() {
var directions = {};
if (this.vx > 0) {
directions.v = DIR.E;
} else if (this.vx < 0) {
directions.v = DIR.W;
}
if (this.vy > 0) {
directions.h = DIR.S;
} else if (this.vy < 0) {
directions.h = DIR.N;
}
return directions;
};
Vector.prototype.magnitude = function() {
return Math.sqrt(this.vx * this.vx + this.vy * this.vy);
};
//----------------------------------------------------------------------------------------
function intersects(r1, r2) {
return r1.x <= r2.x + r2.w && r1.x + r1.w >= r2.x && r1.y <= r2.y + r2.h
&& r1.y + r1.h >= r2.y;
}