0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

optimised Matter.Pairs

This commit is contained in:
liabru 2021-12-06 23:57:09 +00:00
parent f847f4c83e
commit a30707fd87
3 changed files with 47 additions and 76 deletions

View file

@ -62,7 +62,7 @@ Example.manipulation = function() {
var counter = 0, var counter = 0,
scaleFactor = 1.01; scaleFactor = 1.01;
Events.on(engine, 'beforeUpdate', function(event) { Events.on(runner, 'afterTick', function(event) {
counter += 1; counter += 1;
if (counter === 40) if (counter === 40)

View file

@ -12,8 +12,6 @@ var Pair = require('./Pair');
var Common = require('../core/Common'); var Common = require('../core/Common');
(function() { (function() {
Pairs._pairMaxIdleLife = 1000;
/** /**
* Creates a new pairs structure. * Creates a new pairs structure.
@ -40,11 +38,14 @@ var Common = require('../core/Common');
*/ */
Pairs.update = function(pairs, collisions, timestamp) { Pairs.update = function(pairs, collisions, timestamp) {
var pairsList = pairs.list, var pairsList = pairs.list,
pairsListLength = pairsList.length,
pairsTable = pairs.table, pairsTable = pairs.table,
collisionsLength = collisions.length,
collisionStart = pairs.collisionStart, collisionStart = pairs.collisionStart,
collisionEnd = pairs.collisionEnd, collisionEnd = pairs.collisionEnd,
collisionActive = pairs.collisionActive, collisionActive = pairs.collisionActive,
collision, collision,
pairIndex,
pairId, pairId,
pair, pair,
i; i;
@ -54,91 +55,62 @@ var Common = require('../core/Common');
collisionEnd.length = 0; collisionEnd.length = 0;
collisionActive.length = 0; collisionActive.length = 0;
for (i = 0; i < pairsList.length; i++) { for (i = 0; i < pairsListLength; i++) {
pairsList[i].confirmedActive = false; pairsList[i].confirmedActive = false;
} }
for (i = 0; i < collisions.length; i++) { for (i = 0; i < collisionsLength; i++) {
collision = collisions[i]; collision = collisions[i];
pairId = Pair.id(collision.bodyA, collision.bodyB);
pair = pairsTable[pairId];
if (collision.collided) { if (pair) {
pairId = Pair.id(collision.bodyA, collision.bodyB); // pair already exists (but may or may not be active)
if (pair.isActive) {
pair = pairsTable[pairId]; // pair exists and is active
collisionActive.push(pair);
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;
} else { } else {
// pair did not exist, create a new pair // pair exists but was inactive, so a collision has just started again
pair = Pair.create(collision, timestamp);
pairsTable[pairId] = pair;
// push the new pair
collisionStart.push(pair); collisionStart.push(pair);
pairsList.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);
}
}
// find pairs that are no longer active
var removePairIndex = [];
pairsListLength = pairsList.length;
for (i = 0; i < pairsListLength; i++) {
pair = pairsList[i];
if (!pair.confirmedActive) {
Pair.setActive(pair, false, timestamp);
collisionEnd.push(pair);
if (!pair.collision.bodyA.isSleeping && !pair.collision.bodyB.isSleeping) {
removePairIndex.push(i);
} }
} }
} }
// deactivate previously active pairs that are now inactive // remove inactive pairs
for (i = 0; i < pairsList.length; i++) { for (i = 0; i < removePairIndex.length; i++) {
pair = pairsList[i]; pairIndex = removePairIndex[i] - i;
if (pair.isActive && !pair.confirmedActive) {
Pair.setActive(pair, false, timestamp);
collisionEnd.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++) {
pair = pairsList[i];
collision = pair.collision;
// never remove sleeping pairs
if (collision.bodyA.isSleeping || collision.bodyB.isSleeping) {
pair.timeUpdated = timestamp;
continue;
}
// if pair is inactive for too long, mark it to be removed
if (timestamp - pair.timeUpdated > pairMaxIdleLife) {
indexesToRemove.push(i);
}
}
// remove marked pairs
for (i = 0; i < indexesToRemove.length; i++) {
pairIndex = indexesToRemove[i] - i;
pair = pairsList[pairIndex]; pair = pairsList[pairIndex];
delete pairsTable[pair.id];
pairsList.splice(pairIndex, 1); pairsList.splice(pairIndex, 1);
delete pairsTable[pair.id];
} }
}; };

View file

@ -152,7 +152,6 @@ var Body = require('../body/Body');
var pairs = engine.pairs, var pairs = engine.pairs,
timestamp = timing.timestamp; timestamp = timing.timestamp;
Pairs.update(pairs, collisions, timestamp); Pairs.update(pairs, collisions, timestamp);
Pairs.removeOld(pairs, timestamp);
// wake up bodies involved in collisions // wake up bodies involved in collisions
if (engine.enableSleeping) if (engine.enableSleeping)