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:
parent
f847f4c83e
commit
a30707fd87
3 changed files with 47 additions and 76 deletions
|
@ -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)
|
||||||
|
|
|
@ -13,8 +13,6 @@ var Common = require('../core/Common');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
Pairs._pairMaxIdleLife = 1000;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new pairs structure.
|
* Creates a new pairs structure.
|
||||||
* @method create
|
* @method create
|
||||||
|
@ -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,16 +55,13 @@ 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];
|
||||||
|
|
||||||
if (collision.collided) {
|
|
||||||
pairId = Pair.id(collision.bodyA, collision.bodyB);
|
pairId = Pair.id(collision.bodyA, collision.bodyB);
|
||||||
|
|
||||||
pair = pairsTable[pairId];
|
pair = pairsTable[pairId];
|
||||||
|
|
||||||
if (pair) {
|
if (pair) {
|
||||||
|
@ -89,56 +87,30 @@ var Common = require('../core/Common');
|
||||||
pairsList.push(pair);
|
pairsList.push(pair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// deactivate previously active pairs that are now inactive
|
// find pairs that are no longer active
|
||||||
for (i = 0; i < pairsList.length; i++) {
|
var removePairIndex = [];
|
||||||
|
pairsListLength = pairsList.length;
|
||||||
|
|
||||||
|
for (i = 0; i < pairsListLength; i++) {
|
||||||
pair = pairsList[i];
|
pair = pairsList[i];
|
||||||
if (pair.isActive && !pair.confirmedActive) {
|
|
||||||
|
if (!pair.confirmedActive) {
|
||||||
Pair.setActive(pair, false, timestamp);
|
Pair.setActive(pair, false, timestamp);
|
||||||
collisionEnd.push(pair);
|
collisionEnd.push(pair);
|
||||||
|
|
||||||
|
if (!pair.collision.bodyA.isSleeping && !pair.collision.bodyB.isSleeping) {
|
||||||
|
removePairIndex.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
// remove inactive pairs
|
||||||
for (i = 0; i < indexesToRemove.length; i++) {
|
for (i = 0; i < removePairIndex.length; i++) {
|
||||||
pairIndex = indexesToRemove[i] - i;
|
pairIndex = removePairIndex[i] - i;
|
||||||
pair = pairsList[pairIndex];
|
pair = pairsList[pairIndex];
|
||||||
delete pairsTable[pair.id];
|
|
||||||
pairsList.splice(pairIndex, 1);
|
pairsList.splice(pairIndex, 1);
|
||||||
|
delete pairsTable[pair.id];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
Loading…
Reference in a new issue