0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-25 13:39:06 -05:00

optimised Detector.collisions

This commit is contained in:
liabru 2021-11-21 23:52:13 +00:00
parent b3a8aa3cae
commit caeb07e2d2

View file

@ -12,7 +12,6 @@ module.exports = Detector;
var SAT = require('./SAT'); var SAT = require('./SAT');
var Pair = require('./Pair'); var Pair = require('./Pair');
var Bounds = require('../geometry/Bounds');
(function() { (function() {
@ -28,14 +27,14 @@ var Bounds = require('../geometry/Bounds');
pairsTable = engine.pairs.table, pairsTable = engine.pairs.table,
broadphasePairsLength = broadphasePairs.length, broadphasePairsLength = broadphasePairs.length,
canCollide = Detector.canCollide, canCollide = Detector.canCollide,
overlaps = Bounds.overlaps,
collides = SAT.collides, collides = SAT.collides,
pairId = Pair.id, pairId = Pair.id,
i; i;
for (i = 0; i < broadphasePairsLength; i++) { for (i = 0; i < broadphasePairsLength; i++) {
var bodyA = broadphasePairs[i][0], var broadphasePair = broadphasePairs[i],
bodyB = broadphasePairs[i][1]; bodyA = broadphasePair[0],
bodyB = broadphasePair[1];
if ((bodyA.isStatic || bodyA.isSleeping) && (bodyB.isStatic || bodyB.isSleeping)) if ((bodyA.isStatic || bodyA.isSleeping) && (bodyB.isStatic || bodyB.isSleeping))
continue; continue;
@ -43,19 +42,41 @@ var Bounds = require('../geometry/Bounds');
if (!canCollide(bodyA.collisionFilter, bodyB.collisionFilter)) if (!canCollide(bodyA.collisionFilter, bodyB.collisionFilter))
continue; continue;
// mid phase var boundsA = bodyA.bounds,
if (overlaps(bodyA.bounds, bodyB.bounds)) { boundsB = bodyB.bounds;
if (boundsA.min.x > boundsB.max.x || boundsA.max.x < boundsB.min.x
|| boundsA.max.y < boundsB.min.y || boundsA.min.y > boundsB.max.y) {
continue;
}
var partsALength = bodyA.parts.length, var partsALength = bodyA.parts.length,
partsBLength = bodyB.parts.length; partsBLength = bodyB.parts.length;
for (var j = partsALength > 1 ? 1 : 0; j < partsALength; j++) { if (partsALength === 1 && partsBLength === 1) {
var partA = bodyA.parts[j]; var pair = pairsTable[pairId(bodyA, bodyB)];
var collision = collides(bodyA, bodyB, pair && pair.collision, pair && pair.isActive);
for (var k = partsBLength > 1 ? 1 : 0; k < partsBLength; k++) { if (collision.collided) {
var partB = bodyB.parts[k]; collisions.push(collision);
}
} else {
var partsAStart = partsALength > 1 ? 1 : 0,
partsBStart = partsBLength > 1 ? 1 : 0;
for (var j = partsAStart; j < partsALength; j++) {
var partA = bodyA.parts[j],
boundsA = partA.bounds;
for (var k = partsBStart; k < partsBLength; k++) {
var partB = bodyB.parts[k],
boundsB = partB.bounds;
if (boundsA.min.x > boundsB.max.x || boundsA.max.x < boundsB.min.x
|| boundsA.max.y < boundsB.min.y || boundsA.min.y > boundsB.max.y) {
continue;
}
if ((partA === bodyA && partB === bodyB) || overlaps(partA.bounds, partB.bounds)) {
// narrow phase
var pair = pairsTable[pairId(partA, partB)]; var pair = pairsTable[pairId(partA, partB)];
var collision = collides(partA, partB, pair && pair.collision, pair && pair.isActive); var collision = collides(partA, partB, pair && pair.collision, pair && pair.isActive);
@ -66,7 +87,6 @@ var Bounds = require('../geometry/Bounds');
} }
} }
} }
}
return collisions; return collisions;
}; };