This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
collision-resolver.js
485 lines (472 loc) · 14.3 KB
/
collision-resolver.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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import sat from "sat";
import * as three from "three";
import QuadTree from "simple-quadtree";
import decomp from "poly-decomp";
import Map from "es6-map";
// internal SAT polygon conversion helper
function _arrayPolygonToSAT(srcPolygon) {
return new sat.Polygon(
new sat.Vector(),
srcPolygon.map(pt => new sat.Vector(pt[0], pt[1]))
);
}
/**
* Internal point class with quadtree position props and CR helpers
* @property {(string|number)} id - source ID
* @property {three.Vector2} point - vector2 position
* @property {number} x - x property for quadtree
* @property {number} y - y property for quadtree
* @property {number} w - width property for quadtree
* @property {number} h - height property for quadtree
*/
class CRPoint {
/**
* @param {(string|number)} id - source ID of point
* @param {Object} point - source point object (generally a Vector2/3)
* @param {number} point.x - x coordinate of point
* @param {number} point.y - y coordinate of point
*/
constructor (id, point) {
this.id = id;
this.point = new three.Vector2(point.x, point.y);
// assign quadtree props
this.x = point.x;
this.y = point.y;
this.w = 1;
this.h = 1;
}
/**
* @param {Object} position - 2D position with x and y value
* @return {number} - the distance from the specified position
*/
getDist (position) {
return this.point.clone().sub(position).length();
}
/**
* @param {three.Box2} bbox - bbox to check for point containment
* @return {boolean} - true or false indicating containment
*/
isEnclosed (bbox) {
return bbox.containsPoint(this.point);
}
}
/**
* Internal line class with quadtree props and CR helpers
* @property {(string|number)} id - source ID
* @property {three.Vector2} a - start position
* @property {three.Vector2} b - end position
* @property {three.Box2} bbox - 2D bounding box
* @property {number} x - x property for quadtree
* @property {number} y - y property for quadtree
* @property {number} w - width property for quadtree
* @property {number} h - height property for quadtree
* @property {number} relativeWeight - relative weight for snap calculations
*/
class CRLine {
/**
* @param {(string|number)} id - source ID of point
* @param {Object} startPoint - source point object (generally a Vector2/3)
* @param {number} startPoint.x - x coordinate of point
* @param {number} startPoint.y - y coordinate of point
* @param {Object} endPoint - source point object (generally a Vector2/3)
* @param {number} endPoint.x - x coordinate of point
* @param {number} endPoint.y - y coordinate of point
* @param {number} relativeWeight - relative weight for snap calculations
*/
constructor (id, startPoint, endPoint, relativeWeight=0) {
this.id = id;
this.a = new three.Vector2(startPoint.x, startPoint.y);
this.b = new three.Vector2(endPoint.x, endPoint.y);
this.relativeWeight = relativeWeight;
// find bounding box
this.bbox = new three.Box2();
this.bbox.expandByPoint(this.a);
this.bbox.expandByPoint(this.b);
// assign quadtree props
this.x = this.bbox.min.x;
this.y = this.bbox.min.y;
this.w = this.bbox.max.x - this.bbox.min.x;
this.h = this.bbox.max.y - this.bbox.min.y;
}
/**
* Gets the distance to this segment from position
* @param {Object} position - 2D position with x and y value
* @return {number} - the distance from the specified position
*/
getDist (position) {
const delta = position.clone().sub(this.a);
const lineDelta = this.b.clone().sub(this.a);
const lineLength = lineDelta.length();
lineDelta.normalize();
const deltaDist = lineDelta.dot(delta);
if (deltaDist < 0) {
return position.clone().sub(this.a).length();
}
if (deltaDist > lineLength) {
return position.clone().sub(this.b).length();
}
const lineOrthog = new three.Vector2(-lineDelta.y, lineDelta.x);
const orthogDist = lineOrthog.dot(delta);
return Math.abs(orthogDist);
}
/**
* Gets the distance to this segment, and the nearest point along it
* @param {Object} position - 2D position with x and y value
* @return {[number, three.Vector2, three.Vector2]} - the distance, nearest
* point, and vector towards the nearest point on the line
*/
getDistAndNearestPoint (cursorPosition) {
const delta = cursorPosition.clone().sub(this.a);
const lineDelta = this.b.clone().sub(this.a);
const lineLength = lineDelta.length();
lineDelta.normalize();
const deltaDist = lineDelta.dot(delta);
if (deltaDist < 0) {
const resultPoint = this.a.clone();
const resultDelta = resultPoint.clone().sub(cursorPosition);
return [
resultDelta.length(),
resultPoint,
resultDelta.normalize()
];
}
if (deltaDist > lineLength) {
const resultPoint = this.b.clone();
const resultDelta = resultPoint.clone().sub(cursorPosition);
return [
resultDelta.length(),
resultPoint,
resultDelta.normalize()
];
}
const lineOrthog = new three.Vector2(-lineDelta.y, lineDelta.x);
const orthogDist = lineOrthog.dot(delta);
const resultPoint = this.a.clone()
.add(lineDelta.multiplyScalar(deltaDist));
return [
Math.abs(orthogDist),
resultPoint,
resultPoint.clone().sub(cursorPosition).normalize()
];
}
isEnclosed (bbox) {
return bbox.containsBox(this.bbox);
}
}
/**
* Region class for collision resolver
*/
class CRRegion {
constructor (id, points, treeDepth=0) {
this.id = id;
this.points = points.map(p => new three.Vector2(p.x, p.y));
const bbox = this.bbox = new three.Box2();
this.points.forEach(p => this.bbox.expandByPoint(p));
this.x = bbox.min.x;
this.y = bbox.min.y;
this.w = bbox.max.x - bbox.min.x;
this.h = bbox.max.y - bbox.min.y;
this.srcPolygon = points.map(vtx => [vtx.x, vtx.y]);
this.convexParts = [];
this.treeDepth = treeDepth;
decomp.makeCCW(this.srcPolygon);
this.convexParts = decomp
.quickDecomp(this.srcPolygon)
.map(_arrayPolygonToSAT);
}
getDist (cursorPosition) {
let minDist = Number.MAX_VALUE;
const circleWidth = 50;
const circle = new sat.Circle(new sat.Vector(cursorPosition.x, cursorPosition.y), circleWidth);
const satResp = new sat.Response();
this.convexParts.forEach(partSATInstance => {
satResp.clear();
const colliding = sat.testPolygonCircle(partSATInstance, circle, satResp);
if (colliding) {
const dist = -satResp.overlap;
if (dist < minDist) {
minDist = dist;
}
}
});
if (circleWidth + minDist < 0) {
return 1 / (circleWidth + minDist);
}
return (circleWidth + minDist);
}
isEnclosed (bbox) {
return bbox.containsBox(this.bbox);
}
}
/**
* Collision resolver class - keeps track of what's where
*/
export default class CollisionResolver {
constructor (bbox) {
// set up broad-phase collision-checker
this.quadtree = new QuadTree(
bbox.min.x,
bbox.min.y,
bbox.max.x - bbox.min.x,
bbox.max.y - bbox.min.y,
{ maxchildren: 8 }
);
// assign default selection radiuses
this.pointRadius = 10;
this.lineRadius = 4;
// bookkeeping
this.idToEntity = new Map();
}
addPoint (id, srcPoint) {
this.remove(id);
const point = new CRPoint(id, srcPoint);
this.idToEntity[id] = point;
this.quadtree.put(point);
this.idToEntity.set(id, point);
}
addLine (id, srcPoints, relativeWeight) {
this.remove(id);
const entryArr = [];
for (let i=1; i<srcPoints.length; i++) {
const line = new CRLine(id, srcPoints[i-1], srcPoints[i], relativeWeight);
this.quadtree.put(line);
entryArr.push(line);
}
this.idToEntity.set(id, entryArr);
}
addRegion (id, srcPoints, treeDepth=0) {
this.remove(id);
// code smell, but lets us not crash app
if (!srcPoints.length) {
return;
}
const region = new CRRegion(id, srcPoints, treeDepth);
this.quadtree.put(region);
this.idToEntity.set(id, region);
}
addMultiRegion (id, srcRegions) {
this.remove(id);
const entryArr = [];
for (let i=0; i<srcRegions.length; i++) {
const srcPoints = srcRegions[i];
// skip degenerate lines
if (srcPoints.length < 3) {
continue;
}
const region = new CRRegion(id, srcPoints);
this.quadtree.put(region);
entryArr.push(region);
}
this.idToEntity.set(id, entryArr);
}
remove (id) {
const entity = this.idToEntity.get(id);
if (entity) {
if (Array.isArray(entity)) {
entity.forEach(subEntity => this.quadtree.remove(subEntity, "id"));
}
else {
this.quadtree.remove(entity, "id");
}
this.idToEntity.delete(id);
}
}
resolveSelectionBox (bbox, acceptPartialEnclosure = false) {
const qtBBox = {
x: bbox.min.x,
y: bbox.min.y,
w: bbox.max.x - bbox.min.x,
h: bbox.max.y - bbox.min.y
};
const rawHits = this.quadtree.get(qtBBox);
const visitedIDs = {};
const hits = [];
rawHits.forEach(hit => {
if (visitedIDs[hit.id]) {
return;
}
visitedIDs[hit.id] = 1;
const restOfEntity = this.idToEntity.get(hit.id);
if (Array.isArray(restOfEntity)) {
const uncontained = restOfEntity.find(se => !se.isEnclosed(bbox));
if (!uncontained || acceptPartialEnclosure) {
hits.push(hit);
}
}
else if (hit.isEnclosed(bbox) || acceptPartialEnclosure) {
hits.push(hit);
}
});
return hits.map(h => h.id);
}
resolveSelection (cursorPosition, disqualify = null, weights = {}) {
const cursorRadius = Math.max(this.pointRadius, this.lineRadius);
const cursorBBox = {
x: cursorPosition.x - cursorRadius,
y: cursorPosition.y - cursorRadius,
w: cursorRadius * 2,
h: cursorRadius * 2
};
const hits = this.quadtree.get(cursorBBox, cursorRadius);
let bestHitDist = Number.MAX_VALUE;
let bestHit = null;
hits.forEach(item => {
let itemDist = item.getDist(cursorPosition);
if (item instanceof CRLine) {
itemDist -= this.lineRadius;
itemDist -= weights.lines || 0;
}
if (item instanceof CRPoint) {
itemDist -= this.pointRadius;
itemDist -= weights.points || 0;
}
if (item instanceof CRRegion) {
itemDist -= weights.regions || 0;
if (itemDist < 0) {
itemDist -= item.treeDepth;
}
}
if (item.relativeWeight) {
itemDist -= item.relativeWeight;
}
if (disqualify && disqualify(item.id)) {
return;
}
if (itemDist < bestHitDist) {
bestHit = item;
bestHitDist = itemDist;
}
});
if (bestHit) {
if (bestHitDist <= 0) {
return bestHit.id;
}
}
return null;
}
// TODO: test this!
resolveInRadiusWithSnapPositions (
position,
radius = 10,
includePoints = true,
includeLines = true,
pointBias = 1,
lineBias = 1
) {
const hits = this.quadtree.get({
x: position.x - radius,
y: position.y - radius,
w: radius * 2,
h: radius * 2
});
const results = [];
const visitedMultilineIDs = {};
hits.forEach(item => {
if (item instanceof CRPoint) {
if (!includePoints) {
return;
}
const itemDist = item.getDist(position);
if (itemDist <= radius) {
results.push({
id: item.id,
bias: pointBias,
snapDistance: itemDist,
snapPosition: item.point.clone(),
snapOffset: item.point.clone().sub(position)
});
}
}
else if (item instanceof CRLine) {
if (!includeLines) {
return;
}
const restOfLine = this.idToEntity.get(item.id);
if (Array.isArray(restOfLine)) {
if (visitedMultilineIDs[item.id]) {
return;
}
visitedMultilineIDs[item.id] = 1;
let bestSubSnapDist = Infinity;
let bestSubSnap = null;
restOfLine.forEach(subItem => {
const subSnap = subItem.getDistAndNearestPoint(position);
if (subSnap[0] < bestSubSnapDist) {
bestSubSnap = subSnap;
bestSubSnapDist = subSnap[0];
}
});
if (bestSubSnap && bestSubSnapDist <= radius) {
results.push({
id: item.id,
bias: lineBias,
snapDistance: bestSubSnap[0],
snapPosition: bestSubSnap[1],
snapOffset: bestSubSnap[2]
});
}
}
else {
const snap = item.getDistAndNearestPoint(position);
if (snap[0] <= radius) {
results.push({
id: item.id,
bias: lineBias,
snapDistance: snap[0],
snapPosition: snap[1],
snapOffset: snap[2]
});
}
}
}
});
results.sort((a, b) => {
if (a.snapDistance * b.bias > b.snapDistance * a.bias) {
return 1;
}
if (a.snapDistance * b.bias < b.snapDistance * a.bias) {
return -1;
}
return 0;
});
return results;
}
checkCollisions (bbox, id) {
const _doLinesHaveSameEndpoints = (lineA, lineB) => {
return lineA.a.equals(lineB.a)
|| lineA.a.equals(lineB.b)
|| lineA.b.equals(lineB.a)
|| lineA.b.equals(lineB.b);
};
const _doesHitCollide = hit => {
const bboxLineSegment = new sat.Polygon(
new sat.Vector(),
[
new sat.Vector(bbox.min.x, bbox.min.y),
new sat.Vector(bbox.max.x, bbox.max.y)
]);
const hitLineSegment = new sat.Polygon(
new sat.Vector(),
[
new sat.Vector(hit.a.x, hit.a.y),
new sat.Vector(hit.b.x, hit.b.y)
]
);
return sat.testPolygonPolygon(bboxLineSegment, hitLineSegment);
};
const qtBBox = {
x: bbox.min.x,
y: bbox.min.y,
w: bbox.max.x - bbox.min.x,
h: bbox.max.y - bbox.min.y
};
const bboxLine = new CRLine(0, bbox.min, bbox.max);
const rawHits = this.quadtree.get(qtBBox);
const collisions = rawHits.filter(hit => {
return hit instanceof CRLine
&& !_doLinesHaveSameEndpoints(hit, bboxLine)
&& _doesHitCollide(hit);
});
return { id, collisions };
}
}