Skip to content

Commit

Permalink
optimise Detector.collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Apr 29, 2021
1 parent 49fbfba commit c7cec16
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/collision/Detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,22 @@ var Bounds = require('../geometry/Bounds');

// mid phase
if (Bounds.overlaps(bodyA.bounds, bodyB.bounds)) {
for (var j = bodyA.parts.length > 1 ? 1 : 0; j < bodyA.parts.length; j++) {
var partsALength = bodyA.parts.length,
partsBLength = bodyB.parts.length;

for (var j = partsALength > 1 ? 1 : 0; j < partsALength; j++) {
var partA = bodyA.parts[j];

for (var k = bodyB.parts.length > 1 ? 1 : 0; k < bodyB.parts.length; k++) {
for (var k = partsBLength > 1 ? 1 : 0; k < partsBLength; k++) {
var partB = bodyB.parts[k];

if ((partA === bodyA && partB === bodyB) || Bounds.overlaps(partA.bounds, partB.bounds)) {
// find a previous collision we could reuse
var pairId = Pair.id(partA, partB),
pair = pairsTable[pairId],
previousCollision;

if (pair && pair.isActive) {
previousCollision = pair.collision;
} else {
previousCollision = null;
}
pair = pairsTable[pairId];

// narrow phase
var collision = SAT.collides(partA, partB, previousCollision);
var collision = SAT.collides(partA, partB, pair && pair.isActive ? pair.collision : null);

if (collision.collided) {
collisions.push(collision);
Expand Down

0 comments on commit c7cec16

Please sign in to comment.