From 13fde400bacc1b6d2f37a6e08474d5c6270c71c0 Mon Sep 17 00:00:00 2001 From: liabru Date: Mon, 24 Mar 2014 00:47:05 +0000 Subject: [PATCH] Manager has now become the Pairs data structure --- src/collision/Pairs.js | 149 +++++++++++++++++++++++++++++++++++++++++ src/core/Engine.js | 15 ++--- src/module/Outro.js | 2 +- 3 files changed, 154 insertions(+), 12 deletions(-) create mode 100644 src/collision/Pairs.js diff --git a/src/collision/Pairs.js b/src/collision/Pairs.js new file mode 100644 index 0000000..65b818e --- /dev/null +++ b/src/collision/Pairs.js @@ -0,0 +1,149 @@ +/** +* _Internal Class_, not generally used outside of the engine's internals. +* +* @class Pairs +*/ + +var Pairs = {}; + +(function() { + + var _pairMaxIdleLife = 1000; + + /** + * Creates a new pairs structure + * @method create + * @param {object} options + * @return {pairs} A new pairs structure + */ + Pairs.create = function(options) { + return Common.extend({ + table: {}, + list: [], + collisionStart: [], + collisionActive: [], + collisionEnd: [] + }, options); + }; + + /** + * Description + * @method update + * @param {object} pairs + * @param {collision[]} collisions + */ + Pairs.update = function(pairs, collisions, timestamp) { + var pairsList = pairs.list, + pairsTable = pairs.table, + collisionStart = pairs.collisionStart, + collisionEnd = pairs.collisionEnd, + collisionActive = pairs.collisionActive, + activePairIds = [], + collision, + pairId, + pair, + i; + + // clear collision state arrays, but maintain old reference + collisionStart.length = 0; + collisionEnd.length = 0; + collisionActive.length = 0; + + for (i = 0; i < collisions.length; i++) { + collision = collisions[i]; + + if (collision.collided) { + pairId = Pair.id(collision.bodyA, collision.bodyB); + activePairIds.push(pairId); + + if (pairId in pairsTable) { + // pair already exists (but may or may not be active) + pair = pairsTable[pairId]; + + 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); + } 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); + } + } + } + + // deactivate previously active pairs that are now inactive + for (i = 0; i < pairsList.length; i++) { + pair = pairsList[i]; + if (pair.isActive && activePairIds.indexOf(pair.id) === -1) { + Pair.setActive(pair, false, timestamp); + collisionEnd.push(pair); + } + } + }; + + /** + * Description + * @method removeOld + * @param {object} pairs + */ + Pairs.removeOld = function(pairs, timestamp) { + var pairsList = pairs.list, + pairsTable = pairs.table, + indexesToRemove = [], + 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]; + delete pairsTable[pair.id]; + pairsList.splice(pairIndex, 1); + } + }; + + /** + * Clears the given pairs structure + * @method create + * @param {object} options + * @param {pairs} pairs + */ + Pairs.clear = function(pairs) { + pairs.table = {}; + pairs.list.length = 0; + pairs.collisionStart.length = 0; + pairs.collisionActive.length = 0; + pairs.collisionEnd.length = 0; + return pairs; + }; + +})(); \ No newline at end of file diff --git a/src/core/Engine.js b/src/core/Engine.js index 1b3f89f..c211823 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -39,13 +39,6 @@ var Engine = {}; positionIterations: 6, velocityIterations: 4, constraintIterations: 1, - pairs: { - table: {}, - list: [], - collisionStart: [], - collisionActive: [], - collisionEnd: [] - }, enableSleeping: false, timeScale: 1, input: {}, @@ -68,6 +61,7 @@ var Engine = {}; engine.render = engine.render.controller.create(engine.render); engine.world = World.create(engine.world); + engine.pairs = Pairs.create(); engine.metrics = engine.metrics || Metrics.create(); engine.input.mouse = engine.input.mouse || Mouse.create(engine.render.canvas); engine.mouseConstraint = engine.mouseConstraint || MouseConstraint.create(engine.input.mouse); @@ -273,8 +267,8 @@ var Engine = {}; // update pairs var pairs = engine.pairs, timestamp = engine.timing.timestamp; - Manager.updatePairs(pairs, collisions, timestamp); - Manager.removeOldPairs(pairs, timestamp); + Pairs.update(pairs, collisions, timestamp); + Pairs.removeOld(pairs, timestamp); // wake up bodies involved in collisions if (engine.enableSleeping) @@ -340,8 +334,7 @@ var Engine = {}; Engine.clear = function(engine) { var world = engine.world; - engine.pairs.table = {}; - engine.pairs.list = []; + Pairs.clear(engine.pairs); World.addConstraint(engine.world, engine.mouseConstraint.constraint); diff --git a/src/module/Outro.js b/src/module/Outro.js index 1b5f0e5..9ebd2d5 100644 --- a/src/module/Outro.js +++ b/src/module/Outro.js @@ -13,7 +13,7 @@ Matter.World = World; Matter.Contact = Contact; Matter.Detector = Detector; Matter.Grid = Grid; -Matter.Manager = Manager; +Matter.Pairs = Pairs; Matter.Pair = Pair; Matter.Resolver = Resolver; Matter.SAT = SAT;