-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcollide.js
More file actions
275 lines (227 loc) · 7.21 KB
/
collide.js
File metadata and controls
275 lines (227 loc) · 7.21 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
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
/* Circle vs Circle
* INPUT: two circles specified by position and radius:
* c1 = {x:, y:, r:}, c2 = {x:, y:, r:}
* RETURN VALUE:
* false if c1 and c2 do not intersect
* true if c1 and c2 do intersect
*/
function circleCircle(c1,c2) {
var rsum = c1.r + c2.r;
var dx = c1.x - c2.x;
var dy = c1.y - c2.y;
var distsq = dx*dx + dy*dy
return rsum*rsum > distsq;
}
/* Rectangle vs Rectangle
* INPUT: rectangles specified by their minimum and maximum extents:
* r = {min:{x:, y:}, max:{x:, y:}}
* RETURN VALUE:
* false if r1 and r2 do not intersect
* true if r1 and r2 do intersect
*/
function rectangleRectangle(r1, r2) {
return r1.min.x < r2.max.x // no gap in [r2.max.x,r1.min.x]
&& r2.min.x < r1.max.x // no gap in [r1.max.x,r2.min.x]
&& r1.min.y < r2.max.y // no gap in [r2.max.y,r1.min.y]
&& r2.min.y < r1.max.y; // no gap in [r1.max.y,r2.min.y]
}
/* Convex vs Convex
* INPUT: convex polygons as lists of vertices in CCW order
* p = [{x:,y:}, ..., {x:, y:}]
* RETURN VALUE:
* false if p1 and p2 do not intersect
* true if p1 and p2 do intersect
*/
function convexConvex(p1, p2) {
return convexConvexHelper(p1, p2) && convexConvexHelper(p2, p1)
}
function convexConvexHelper(p1, p2) {
for (var i = 0; i < p1.length; i++) {
var x1 = p1[i].x;
var y1 = p1[i].y;
var x2 = p1[(i+1) % p1.length].x;
var y2 = p1[(i+1) % p1.length].y;
var x3 = p1[(i+2) % p1.length].x;
var y3 = p1[(i+2) % p1.length].y;
var p1Side = lineSide(x1, y1, x2, y2, x3, y3);
var p2Side = 0;
for (var j = 0; j < p2.length; j++) {
var p2x = p2[j].x;
var p2y = p2[j].y;
p2Side = lineSide(x1, y1, x2, y2, p2x, p2y);
if (p1Side == p2Side) {
break;
}
}
if (p1Side != p2Side) {
return false;
}
}
return true;
}
function circleConvex(circle, convex){
var radius = circle.r;
var radiusSq = radius * radius;
for (var i = 0; i < convex.length; i++) {
var x1 = convex[i].x;
var y1 = convex[i].y;
var dx = circle.x - x1;
var dy = circle.y - y1;
var distsq = dx*dx + dy*dy
if (distsq <= radiusSq) return true;
var x2 = convex[(i+1) % convex.length].x;
var y2 = convex[(i+1) % convex.length].y;
var x3 = convex[(i+2) % convex.length].x;
var y3 = convex[(i+2) % convex.length].y;
var convexSide = lineSide(x1, y1, x2, y2, x3, y3);
var circleSide = lineSide(x1, y1, x2, y2, circle.x, circle.y);
if (convexSide != circleSide) {
return false;
}
}
return true;
}
function lineSide(x1, y1, x2, y2, px, py) {
return Math.sign((x1-x2)*(py-y1) - (y1-y2)*(px-x1));
}
/* Rav vs Circle
* INPUT: ray specified as a start and end point, circle as above.
* ray = {start:{x:, y:}, end:{x:, y:}}
* RETURN VALUE:
* null if no intersection
* {t:} if intersection
* -- NOTE: 0.0 <= t <= 1.0 gives the position of the first intersection
*/
function rayCircle(r, c) {
// Reference: http://math.stackexchange.com/questions/311921/get-location-of-vector-circle-intersection
var ray_dx = r.end.x - r.start.x;
var ray_dy = r.end.y - r.start.y;
var A = ray_dx * ray_dx + ray_dy * ray_dy;
var rel_circlex = r.start.x - c.x;
var rel_circley = r.start.y - c.y;
var B = 2 * (ray_dx * rel_circlex + ray_dy * rel_circley);
var C = rel_circlex * rel_circlex + rel_circley * rel_circley - c.r * c.r;
var discriminant = B * B - 4 * A * C;
if (discriminant < 0) {
return null;
}
else {
var T = (-B - Math.sqrt(discriminant))/(2*A)
var T2 = (-B + Math.sqrt(discriminant))/(2*A);
if (T > 0 && T < 1) {
return {t:T};
}
else if (T2 > 0 && T2 < 1) {
return {t:T2};
}
}
return null;
}
/* Rav vs Rectangle
* INPUT: ray as above, rectangle as above.
* RETURN VALUE:
* null if no intersection
* {t:} if intersection
* -- NOTE: 0.0 <= t <= 1.0 gives the position of the first intersection
*/
function rayRectangle(r, b) {
var dx = r.end.x - r.start.x;
var dy = r.end.y - r.start.y;
var minx = b.min.x - r.start.x;
var miny = b.min.y - r.start.y;
var maxx = b.max.x - r.start.x;
var maxy = b.max.y - r.start.y;
var tx1 = minx / dx;
var tx2 = maxx / dx;
var ty1 = miny / dy;
var ty2 = maxy / dy;
var txmin = Math.min(tx1,tx2);
var txmax = Math.max(tx1,tx2);
var tymin = Math.min(ty1,ty2);
var tymax = Math.max(ty1,ty2);
var min = Math.max(txmin,tymin);
var max = Math.min(txmax,tymax);
if(min < max && min <= 1 && max >= 0) {
return {t: Math.max(0,min)};
}
else {
return null;
}
}
/* Rav vs Convex
* INPUT: ray as above, convex polygon as above.
* RETURN VALUE:
* null if no intersection
* {t:} if intersection
* -- NOTE: 0.0 <= t <= 1.0 gives the position of the first intersection
*/
/* Rav vs Convex
* INPUT: ray as above, convex polygon as above.
* RETURN VALUE:
* null if no intersection
* {t:} if intersection
* -- NOTE: 0.0 <= t <= 1.0 gives the position of the first intersection
*/
function rayConvex(r, p) {
var ray_dx = r.end.x - r.start.x;
var ray_dy = r.end.y - r.start.y;
var T_enter = Number.NEGATIVE_INFINITY;
var T_exit = Number.POSITIVE_INFINITY;
for (var i = 0; i < p.length; i++) {
var startx = p[i].x;
var starty = p[i].y;
var endx = p[(i+1)%p.length].x;
var endy = p[(i+1)%p.length].y;
var norm_dx = starty - endy;
var norm_dy = endx - startx;
// Enter/exiting reference: http://geomalgorithms.com/a13-_intersect-4.html
var dot = ray_dx * norm_dx + ray_dy * norm_dy;
var slope = (endy - starty)/(endx - startx);
var T_numerator = (r.start.x - startx) * slope + starty - r.start.y;
var T_denominator = ray_dy - slope * ray_dx;
var T_intersect = T_numerator / T_denominator;
if (dot > 0) {
if (T_intersect > T_enter) {
T_enter = T_intersect;
}
}
else {
if (T_intersect < T_exit) {
T_exit = T_intersect;
}
}
}
if (T_enter > T_exit) {
return null;
}
if (T_enter > 0 && T_enter < 1) {
return {t:T_enter};
}
else if (T_exit > 0 && T_exit < 1) {
return {t:T_exit};
}
return null;
}
function convertRectToConvex(rect){
var x1 = rect.min.x;
var y1 = rect.min.y;
var x2 = rect.max.x;
var y2 = rect.max.y;
var convex = [
{x : x1, y : y1},
{x : x1, y : y2},
{x : x2, y : y2},
{x : x2, y : y1}
];
return convex;
}
module.exports = {
circleCircle: circleCircle,
rectangleRectangle: rectangleRectangle,
convexConvex: convexConvex,
rayCircle: rayCircle,
rayRectangle: rayRectangle,
rayConvex: rayConvex,
circleConvex: circleConvex,
convertRectToConvex:convertRectToConvex,
};