From caeb07e2d242ebad4207b41fd7dd63e81a410e5c Mon Sep 17 00:00:00 2001 From: liabru Date: Sun, 21 Nov 2021 23:52:13 +0000 Subject: [PATCH] optimised Detector.collisions --- src/collision/Detector.js | 58 ++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/src/collision/Detector.js b/src/collision/Detector.js index 9b4e1bc..d1affe5 100644 --- a/src/collision/Detector.js +++ b/src/collision/Detector.js @@ -12,7 +12,6 @@ module.exports = Detector; var SAT = require('./SAT'); var Pair = require('./Pair'); -var Bounds = require('../geometry/Bounds'); (function() { @@ -28,14 +27,14 @@ var Bounds = require('../geometry/Bounds'); pairsTable = engine.pairs.table, broadphasePairsLength = broadphasePairs.length, canCollide = Detector.canCollide, - overlaps = Bounds.overlaps, collides = SAT.collides, pairId = Pair.id, i; for (i = 0; i < broadphasePairsLength; i++) { - var bodyA = broadphasePairs[i][0], - bodyB = broadphasePairs[i][1]; + var broadphasePair = broadphasePairs[i], + bodyA = broadphasePair[0], + bodyB = broadphasePair[1]; if ((bodyA.isStatic || bodyA.isSleeping) && (bodyB.isStatic || bodyB.isSleeping)) continue; @@ -43,25 +42,46 @@ var Bounds = require('../geometry/Bounds'); if (!canCollide(bodyA.collisionFilter, bodyB.collisionFilter)) continue; - // mid phase - if (overlaps(bodyA.bounds, bodyB.bounds)) { - var partsALength = bodyA.parts.length, - partsBLength = bodyB.parts.length; + var boundsA = bodyA.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, + partsBLength = bodyB.parts.length; + + if (partsALength === 1 && partsBLength === 1) { + var pair = pairsTable[pairId(bodyA, bodyB)]; + var collision = collides(bodyA, bodyB, pair && pair.collision, pair && pair.isActive); + + if (collision.collided) { + collisions.push(collision); + } + } else { + var partsAStart = partsALength > 1 ? 1 : 0, + partsBStart = partsBLength > 1 ? 1 : 0; - for (var j = partsALength > 1 ? 1 : 0; j < partsALength; j++) { - var partA = bodyA.parts[j]; + for (var j = partsAStart; j < partsALength; j++) { + var partA = bodyA.parts[j], + boundsA = partA.bounds; - for (var k = partsBLength > 1 ? 1 : 0; k < partsBLength; k++) { - var partB = bodyB.parts[k]; + for (var k = partsBStart; k < partsBLength; k++) { + var partB = bodyB.parts[k], + boundsB = partB.bounds; - if ((partA === bodyA && partB === bodyB) || overlaps(partA.bounds, partB.bounds)) { - // narrow phase - var pair = pairsTable[pairId(partA, partB)]; - var collision = collides(partA, partB, pair && pair.collision, pair && pair.isActive); + 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 (collision.collided) { - collisions.push(collision); - } + var pair = pairsTable[pairId(partA, partB)]; + var collision = collides(partA, partB, pair && pair.collision, pair && pair.isActive); + + if (collision.collided) { + collisions.push(collision); } } }