0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

improved performance by passing timestamps

This commit is contained in:
liabru 2014-03-14 21:14:21 +00:00
parent ec06592fcd
commit 2b5eb92556
3 changed files with 26 additions and 23 deletions

View file

@ -16,7 +16,7 @@ var Manager = {};
* @param {object} pairs
* @param {collision[]} collisions
*/
Manager.updatePairs = function(pairs, collisions) {
Manager.updatePairs = function(pairs, collisions, timestamp) {
var pairsList = pairs.list,
pairsTable = pairs.table,
collisionStart = pairs.collisionStart,
@ -53,10 +53,10 @@ var Manager = {};
}
// update the pair
Pair.update(pair, collision);
Pair.update(pair, collision, timestamp);
} else {
// pair did not exist, create a new pair
pair = Pair.create(collision);
pair = Pair.create(collision, timestamp);
pairsTable[pairId] = pair;
// push the new pair
@ -70,7 +70,7 @@ var Manager = {};
for (i = 0; i < pairsList.length; i++) {
pair = pairsList[i];
if (pair.isActive && activePairIds.indexOf(pair.id) === -1) {
Pair.setActive(pair, false);
Pair.setActive(pair, false, timestamp);
collisionEnd.push(pair);
}
}
@ -81,10 +81,9 @@ var Manager = {};
* @method removeOldPairs
* @param {object} pairs
*/
Manager.removeOldPairs = function(pairs) {
Manager.removeOldPairs = function(pairs, timestamp) {
var pairsList = pairs.list,
pairsTable = pairs.table,
timeNow = Common.now(),
indexesToRemove = [],
pair,
collision,
@ -97,12 +96,12 @@ var Manager = {};
// never remove sleeping pairs
if (collision.bodyA.isSleeping || collision.bodyB.isSleeping) {
pair.timeUpdated = timeNow;
pair.timeUpdated = timestamp;
continue;
}
// if pair is inactive for too long, mark it to be removed
if (timeNow - pair.timeUpdated > _pairMaxIdleLife) {
if (timestamp - pair.timeUpdated > _pairMaxIdleLife) {
indexesToRemove.push(i);
}
}

View file

@ -14,10 +14,9 @@ var Pair = {};
* @param {collision} collision
* @return {pair} A new pair
*/
Pair.create = function(collision) {
Pair.create = function(collision, timestamp) {
var bodyA = collision.bodyA,
bodyB = collision.bodyB,
timestamp = Common.now();
bodyB = collision.bodyB;
var pair = {
id: Pair.id(bodyA, bodyB),
@ -35,7 +34,7 @@ var Pair = {};
slop: Math.max(bodyA.slop, bodyB.slop)
};
Pair.update(pair, collision);
Pair.update(pair, collision, timestamp);
return pair;
};
@ -46,12 +45,13 @@ var Pair = {};
* @param {pair} pair
* @param {collision} collision
*/
Pair.update = function(pair, collision) {
Pair.update = function(pair, collision, timestamp) {
var contacts = pair.contacts,
supports = collision.supports,
activeContacts = [];
activeContacts = pair.activeContacts;
pair.collision = collision;
activeContacts.length = 0;
if (collision.collided) {
for (var i = 0; i < supports.length; i++) {
@ -65,11 +65,11 @@ var Pair = {};
}
}
pair.activeContacts = activeContacts;
pair.separation = collision.depth;
Pair.setActive(pair, true);
Pair.setActive(pair, true, timestamp);
} else {
Pair.setActive(pair, false);
if (pair.isActive === true)
Pair.setActive(pair, false, timestamp);
}
};
@ -79,13 +79,13 @@ var Pair = {};
* @param {pair} pair
* @param {bool} isActive
*/
Pair.setActive = function(pair, isActive) {
Pair.setActive = function(pair, isActive, timestamp) {
if (isActive) {
pair.isActive = true;
pair.timeUpdated = Common.now();
pair.timeUpdated = timestamp;
} else {
pair.isActive = false;
pair.activeContacts = [];
pair.activeContacts.length = 0;
}
};

View file

@ -106,6 +106,9 @@ var Engine = {};
if (!engine.enabled)
return;
// timestamp is undefined on the first update
timestamp = timestamp || 0;
// create an event object
var event = {
timestamp: timestamp
@ -264,9 +267,10 @@ var Engine = {};
var collisions = broadphase.detector(broadphasePairs, engine.metrics);
// update pairs
var pairs = engine.pairs;
Manager.updatePairs(pairs, collisions);
Manager.removeOldPairs(pairs);
var pairs = engine.pairs,
timestamp = engine.timing.timestamp;
Manager.updatePairs(pairs, collisions, timestamp);
Manager.removeOldPairs(pairs, timestamp);
// wake up bodies involved in collisions
if (engine.enableSleeping)