-
Notifications
You must be signed in to change notification settings - Fork 0
/
collision.js
270 lines (218 loc) · 6.43 KB
/
collision.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
const QuadTree = require("@timohausmann/quadtree-js");
class Vector {
constructor(xOrVector = 0, y = 0) {
if (xOrVector instanceof Vector) {
const vector = xOrVector;
this.x = vector.x;
this.y = vector.y;
} else {
const x = xOrVector;
this.x = x;
this.y = y;
}
}
dotProduct({ x, y }) {
return this.x * x + this.y * y;
}
add({ x, y }) {
this.x += x;
this.y += y;
return this;
}
sub({ x, y }) {
this.x -= x;
this.y -= y;
return this;
}
perp() {
const x = this.x;
this.x = this.y;
this.y = -x;
return this;
}
div(n) {
this.x /= n;
this.y /= n;
return this;
}
get length2() {
return this.dotProduct(this);
}
get length() {
return Math.sqrt(this.length2);
}
distance({ x, y }) {
return Math.sqrt((this.x - x) ** 2 + (this.y - y) ** 2);
}
normalize() {
return this.div(this.length);
}
abs() {
if (this.x < 0) this.x *= -1;
if (this.y < 0) this.y *= -1;
return this;
}
}
class Line {
constructor(p1, p2) {
this.points = [p1, p2];
const edge = new Vector(p2).sub(p1);
this.normals = [new Vector(edge).perp().normalize()];
this.bounds = new Rect(p1, p2);
}
}
const RECT_KEYS = [/*"x", "y",*/ "width", "height"];
const isRect = (obj) => typeof obj === "object" && RECT_KEYS.every((k) => k in obj);
class Rect {
constructor(xOrP1OrRect = 0, yOrP2 = 0, width = 0, height = 0) {
if (isRect(xOrP1OrRect)) {
const rect = xOrP1OrRect;
this.x = rect.x || 0;
this.y = rect.y || 0;
this.width = rect.width;
this.height = rect.height;
} else if (xOrP1OrRect instanceof Vector && yOrP2 instanceof Vector) {
const p1 = xOrP1OrRect;
const p2 = yOrP2;
this.x = Math.min(p1.x, p2.x);
this.y = Math.min(p1.y, p2.y);
const delta = new Vector(p2).sub(p1).abs();
this.width = delta.x;
this.height = delta.y;
} else {
const x = xOrP1OrRect;
const y = yOrP2;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
this.points = [
new Vector(this.x, this.y),
new Vector(this.x + this.width, this.y),
new Vector(this.x + this.width, this.y + this.height),
new Vector(this.x, this.y + this.height),
];
this.normals = this.points.map((p, i, ps) => new Vector(ps[(i + 1) % 4]).sub(p).perp().normalize());
this.left = this.x;
this.right = this.x + this.width;
this.top = this.y;
this.bottom = this.y + this.height;
}
overlaps({ top, right, bottom, left }) {
return this.left < right && left < this.right && this.top < bottom && top < this.bottom;
}
union({ top, right, bottom, left }) {
const x = Math.min(this.left, left);
const y = Math.min(this.top, top);
const width = Math.max(this.right, right) - x;
const height = Math.max(this.bottom, bottom) - y;
return new Rect(x, y, width, height);
}
intersect({ top, right, bottom, left }) {
if (this.top >= top && this.right <= right && this.bottom <= bottom && this.left >= left) {
return this;
}
const x = Math.max(this.left, left);
const y = Math.max(this.top, top);
const width = Math.min(this.right, right) - x;
const height = Math.min(this.bottom, bottom) - y;
return new Rect(x, y, width, height);
}
}
function flattenPointsOn(points, normal) {
var min = Number.MAX_VALUE;
var max = -Number.MAX_VALUE;
var len = points.length;
for (var i = 0; i < len; i++) {
// The magnitude of the projection of the point onto the normal
var dot = points[i].dotProduct(normal);
if (dot < min) {
min = dot;
}
if (dot > max) {
max = dot;
}
}
return [min, max];
}
function isSeparatingAxis(aPoints, bPoints, axis) {
// Project the polygons onto the axis.
const rangeA = flattenPointsOn(aPoints, axis);
const rangeB = flattenPointsOn(bPoints, axis);
// The magnitude of the offset between the two polygons
//const offsetV = new Vector(bPos).sub(aPos);
const projectedOffset = new Vector().dotProduct(axis);
// Move B's range to its position relative to A.
rangeB[0] += projectedOffset;
rangeB[1] += projectedOffset;
// Check if there is a gap. If there is, this is a separating axis and we can stop
return rangeA[0] > rangeB[1] || rangeB[0] > rangeA[1];
}
function testShapes(polyA, polyB) {
const aPoints = polyA.points;
const aNormals = polyA.normals;
const aLen = aNormals.length;
const bPoints = polyB.points;
const bNormals = polyB.normals;
const bLen = bNormals.length;
// If any of the edge normals of A is a separating axis, no intersection.
for (let i = 0; i < aLen; i++) {
if (isSeparatingAxis(aPoints, bPoints, aNormals[i])) {
return false;
}
}
// If any of the edge normals of B is a separating axis, no intersection.
for (let i = 0; i < bLen; i++) {
if (isSeparatingAxis(aPoints, bPoints, bNormals[i])) {
return false;
}
}
return true;
}
class Container extends QuadTree {
constructor(obstacles) {
obstacles = obstacles.map((o) => new Rect(o));
super(obstacles.reduce((bounds, o) => bounds.union(o), new Rect()));
obstacles.forEach((o) => this.insert(o));
this.broadphase = [];
}
rayCast(line) {
// retrieve a list of potential colliding objects
const candidates = this.retrieve(line.bounds);
this.broadphase.push({ line, candidates });
const result = [];
for (let obstacle of candidates) {
// fast AABB check if both bounding boxes are overlapping
if (line.bounds.overlaps(obstacle)) {
// full SAT collision check
if (testShapes(line, obstacle)) {
// we touched something !
result.push(obstacle);
}
}
}
return result;
}
hasLOS(line) {
return this.rayCast(line).length === 0;
}
hasFreeLOS([p1, p2], pad = 0) {
if (pad) {
return (
this.hasLOS(new Line(p1, p2)) &&
this.hasLOS(new Line(new Vector(+pad, +pad).add(p1), new Vector(+pad, +pad).add(p2))) &&
this.hasLOS(new Line(new Vector(-pad, +pad).add(p1), new Vector(-pad, +pad).add(p2))) &&
this.hasLOS(new Line(new Vector(-pad, -pad).add(p1), new Vector(-pad, -pad).add(p2))) &&
this.hasLOS(new Line(new Vector(+pad, -pad).add(p1), new Vector(+pad, -pad).add(p2)))
);
}
return this.hasLOS(new Line(p1, p2));
}
}
module.exports = Container;
Object.assign(module.exports, {
Vector,
Line,
Rect,
});