Skip to content

Commit

Permalink
optimised Matter.Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Dec 6, 2021
1 parent f847f4c commit a30707f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 70 deletions.
2 changes: 1 addition & 1 deletion examples/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Example.manipulation = function() {
var counter = 0,
scaleFactor = 1.01;

Events.on(engine, 'beforeUpdate', function(event) {
Events.on(runner, 'afterTick', function(event) {
counter += 1;

if (counter === 40)
Expand Down
108 changes: 40 additions & 68 deletions src/collision/Pairs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ var Pair = require('./Pair');
var Common = require('../core/Common');

(function() {

Pairs._pairMaxIdleLife = 1000;

/**
* Creates a new pairs structure.
Expand All @@ -40,11 +38,14 @@ var Common = require('../core/Common');
*/
Pairs.update = function(pairs, collisions, timestamp) {
var pairsList = pairs.list,
pairsListLength = pairsList.length,
pairsTable = pairs.table,
collisionsLength = collisions.length,
collisionStart = pairs.collisionStart,
collisionEnd = pairs.collisionEnd,
collisionActive = pairs.collisionActive,
collision,
pairIndex,
pairId,
pair,
i;
Expand All @@ -54,91 +55,62 @@ var Common = require('../core/Common');
collisionEnd.length = 0;
collisionActive.length = 0;

for (i = 0; i < pairsList.length; i++) {
for (i = 0; i < pairsListLength; i++) {
pairsList[i].confirmedActive = false;
}

for (i = 0; i < collisions.length; i++) {
for (i = 0; i < collisionsLength; i++) {
collision = collisions[i];

if (collision.collided) {
pairId = Pair.id(collision.bodyA, collision.bodyB);

pair = pairsTable[pairId];

if (pair) {
// pair already exists (but may or may not be active)
if (pair.isActive) {
// pair exists and is active
collisionActive.push(pair);
} else {
// pair exists but was inactive, so a collision has just started again
collisionStart.push(pair);
}

// update the pair
Pair.update(pair, collision, timestamp);
pair.confirmedActive = true;
pairId = Pair.id(collision.bodyA, collision.bodyB);
pair = pairsTable[pairId];

if (pair) {
// pair already exists (but may or may not be active)
if (pair.isActive) {
// pair exists and is active
collisionActive.push(pair);
} else {
// pair did not exist, create a new pair
pair = Pair.create(collision, timestamp);
pairsTable[pairId] = pair;

// push the new pair
// pair exists but was inactive, so a collision has just started again
collisionStart.push(pair);
pairsList.push(pair);
}
}
}

// deactivate previously active pairs that are now inactive
for (i = 0; i < pairsList.length; i++) {
pair = pairsList[i];
if (pair.isActive && !pair.confirmedActive) {
Pair.setActive(pair, false, timestamp);
collisionEnd.push(pair);
// update the pair
Pair.update(pair, collision, timestamp);
pair.confirmedActive = true;
} else {
// pair did not exist, create a new pair
pair = Pair.create(collision, timestamp);
pairsTable[pairId] = pair;

// push the new pair
collisionStart.push(pair);
pairsList.push(pair);
}
}
};

/**
* Finds and removes pairs that have been inactive for a set amount of time.
* @method removeOld
* @param {object} pairs
* @param {number} timestamp
*/
Pairs.removeOld = function(pairs, timestamp) {
var pairsList = pairs.list,
pairsTable = pairs.table,
indexesToRemove = [],
pairMaxIdleLife = Pairs._pairMaxIdleLife,
pair,
collision,
pairIndex,
i;

for (i = 0; i < pairsList.length; i++) {
// find pairs that are no longer active
var removePairIndex = [];
pairsListLength = pairsList.length;

for (i = 0; i < pairsListLength; i++) {
pair = pairsList[i];
collision = pair.collision;

// never remove sleeping pairs
if (collision.bodyA.isSleeping || collision.bodyB.isSleeping) {
pair.timeUpdated = timestamp;
continue;
}
if (!pair.confirmedActive) {
Pair.setActive(pair, false, timestamp);
collisionEnd.push(pair);

// if pair is inactive for too long, mark it to be removed
if (timestamp - pair.timeUpdated > pairMaxIdleLife) {
indexesToRemove.push(i);
if (!pair.collision.bodyA.isSleeping && !pair.collision.bodyB.isSleeping) {
removePairIndex.push(i);
}
}
}

// remove marked pairs
for (i = 0; i < indexesToRemove.length; i++) {
pairIndex = indexesToRemove[i] - i;
// remove inactive pairs
for (i = 0; i < removePairIndex.length; i++) {
pairIndex = removePairIndex[i] - i;
pair = pairsList[pairIndex];
delete pairsTable[pair.id];
pairsList.splice(pairIndex, 1);
delete pairsTable[pair.id];
}
};

Expand Down
1 change: 0 additions & 1 deletion src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ var Body = require('../body/Body');
var pairs = engine.pairs,
timestamp = timing.timestamp;
Pairs.update(pairs, collisions, timestamp);
Pairs.removeOld(pairs, timestamp);

// wake up bodies involved in collisions
if (engine.enableSleeping)
Expand Down

0 comments on commit a30707f

Please sign in to comment.