mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
improved performance by passing timestamps
This commit is contained in:
parent
ec06592fcd
commit
2b5eb92556
3 changed files with 26 additions and 23 deletions
|
@ -16,7 +16,7 @@ var Manager = {};
|
||||||
* @param {object} pairs
|
* @param {object} pairs
|
||||||
* @param {collision[]} collisions
|
* @param {collision[]} collisions
|
||||||
*/
|
*/
|
||||||
Manager.updatePairs = function(pairs, collisions) {
|
Manager.updatePairs = function(pairs, collisions, timestamp) {
|
||||||
var pairsList = pairs.list,
|
var pairsList = pairs.list,
|
||||||
pairsTable = pairs.table,
|
pairsTable = pairs.table,
|
||||||
collisionStart = pairs.collisionStart,
|
collisionStart = pairs.collisionStart,
|
||||||
|
@ -53,10 +53,10 @@ var Manager = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// update the pair
|
// update the pair
|
||||||
Pair.update(pair, collision);
|
Pair.update(pair, collision, timestamp);
|
||||||
} else {
|
} else {
|
||||||
// pair did not exist, create a new pair
|
// pair did not exist, create a new pair
|
||||||
pair = Pair.create(collision);
|
pair = Pair.create(collision, timestamp);
|
||||||
pairsTable[pairId] = pair;
|
pairsTable[pairId] = pair;
|
||||||
|
|
||||||
// push the new pair
|
// push the new pair
|
||||||
|
@ -70,7 +70,7 @@ var Manager = {};
|
||||||
for (i = 0; i < pairsList.length; i++) {
|
for (i = 0; i < pairsList.length; i++) {
|
||||||
pair = pairsList[i];
|
pair = pairsList[i];
|
||||||
if (pair.isActive && activePairIds.indexOf(pair.id) === -1) {
|
if (pair.isActive && activePairIds.indexOf(pair.id) === -1) {
|
||||||
Pair.setActive(pair, false);
|
Pair.setActive(pair, false, timestamp);
|
||||||
collisionEnd.push(pair);
|
collisionEnd.push(pair);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,10 +81,9 @@ var Manager = {};
|
||||||
* @method removeOldPairs
|
* @method removeOldPairs
|
||||||
* @param {object} pairs
|
* @param {object} pairs
|
||||||
*/
|
*/
|
||||||
Manager.removeOldPairs = function(pairs) {
|
Manager.removeOldPairs = function(pairs, timestamp) {
|
||||||
var pairsList = pairs.list,
|
var pairsList = pairs.list,
|
||||||
pairsTable = pairs.table,
|
pairsTable = pairs.table,
|
||||||
timeNow = Common.now(),
|
|
||||||
indexesToRemove = [],
|
indexesToRemove = [],
|
||||||
pair,
|
pair,
|
||||||
collision,
|
collision,
|
||||||
|
@ -97,12 +96,12 @@ var Manager = {};
|
||||||
|
|
||||||
// never remove sleeping pairs
|
// never remove sleeping pairs
|
||||||
if (collision.bodyA.isSleeping || collision.bodyB.isSleeping) {
|
if (collision.bodyA.isSleeping || collision.bodyB.isSleeping) {
|
||||||
pair.timeUpdated = timeNow;
|
pair.timeUpdated = timestamp;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if pair is inactive for too long, mark it to be removed
|
// 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);
|
indexesToRemove.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,10 +14,9 @@ var Pair = {};
|
||||||
* @param {collision} collision
|
* @param {collision} collision
|
||||||
* @return {pair} A new pair
|
* @return {pair} A new pair
|
||||||
*/
|
*/
|
||||||
Pair.create = function(collision) {
|
Pair.create = function(collision, timestamp) {
|
||||||
var bodyA = collision.bodyA,
|
var bodyA = collision.bodyA,
|
||||||
bodyB = collision.bodyB,
|
bodyB = collision.bodyB;
|
||||||
timestamp = Common.now();
|
|
||||||
|
|
||||||
var pair = {
|
var pair = {
|
||||||
id: Pair.id(bodyA, bodyB),
|
id: Pair.id(bodyA, bodyB),
|
||||||
|
@ -35,7 +34,7 @@ var Pair = {};
|
||||||
slop: Math.max(bodyA.slop, bodyB.slop)
|
slop: Math.max(bodyA.slop, bodyB.slop)
|
||||||
};
|
};
|
||||||
|
|
||||||
Pair.update(pair, collision);
|
Pair.update(pair, collision, timestamp);
|
||||||
|
|
||||||
return pair;
|
return pair;
|
||||||
};
|
};
|
||||||
|
@ -46,12 +45,13 @@ var Pair = {};
|
||||||
* @param {pair} pair
|
* @param {pair} pair
|
||||||
* @param {collision} collision
|
* @param {collision} collision
|
||||||
*/
|
*/
|
||||||
Pair.update = function(pair, collision) {
|
Pair.update = function(pair, collision, timestamp) {
|
||||||
var contacts = pair.contacts,
|
var contacts = pair.contacts,
|
||||||
supports = collision.supports,
|
supports = collision.supports,
|
||||||
activeContacts = [];
|
activeContacts = pair.activeContacts;
|
||||||
|
|
||||||
pair.collision = collision;
|
pair.collision = collision;
|
||||||
|
activeContacts.length = 0;
|
||||||
|
|
||||||
if (collision.collided) {
|
if (collision.collided) {
|
||||||
for (var i = 0; i < supports.length; i++) {
|
for (var i = 0; i < supports.length; i++) {
|
||||||
|
@ -65,11 +65,11 @@ var Pair = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pair.activeContacts = activeContacts;
|
|
||||||
pair.separation = collision.depth;
|
pair.separation = collision.depth;
|
||||||
Pair.setActive(pair, true);
|
Pair.setActive(pair, true, timestamp);
|
||||||
} else {
|
} else {
|
||||||
Pair.setActive(pair, false);
|
if (pair.isActive === true)
|
||||||
|
Pair.setActive(pair, false, timestamp);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -79,13 +79,13 @@ var Pair = {};
|
||||||
* @param {pair} pair
|
* @param {pair} pair
|
||||||
* @param {bool} isActive
|
* @param {bool} isActive
|
||||||
*/
|
*/
|
||||||
Pair.setActive = function(pair, isActive) {
|
Pair.setActive = function(pair, isActive, timestamp) {
|
||||||
if (isActive) {
|
if (isActive) {
|
||||||
pair.isActive = true;
|
pair.isActive = true;
|
||||||
pair.timeUpdated = Common.now();
|
pair.timeUpdated = timestamp;
|
||||||
} else {
|
} else {
|
||||||
pair.isActive = false;
|
pair.isActive = false;
|
||||||
pair.activeContacts = [];
|
pair.activeContacts.length = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,9 @@ var Engine = {};
|
||||||
if (!engine.enabled)
|
if (!engine.enabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// timestamp is undefined on the first update
|
||||||
|
timestamp = timestamp || 0;
|
||||||
|
|
||||||
// create an event object
|
// create an event object
|
||||||
var event = {
|
var event = {
|
||||||
timestamp: timestamp
|
timestamp: timestamp
|
||||||
|
@ -264,9 +267,10 @@ var Engine = {};
|
||||||
var collisions = broadphase.detector(broadphasePairs, engine.metrics);
|
var collisions = broadphase.detector(broadphasePairs, engine.metrics);
|
||||||
|
|
||||||
// update pairs
|
// update pairs
|
||||||
var pairs = engine.pairs;
|
var pairs = engine.pairs,
|
||||||
Manager.updatePairs(pairs, collisions);
|
timestamp = engine.timing.timestamp;
|
||||||
Manager.removeOldPairs(pairs);
|
Manager.updatePairs(pairs, collisions, timestamp);
|
||||||
|
Manager.removeOldPairs(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