mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-27 13:59:01 -05:00
Manager has now become the Pairs data structure
This commit is contained in:
parent
2d52d53bf0
commit
13fde400ba
3 changed files with 154 additions and 12 deletions
149
src/collision/Pairs.js
Normal file
149
src/collision/Pairs.js
Normal file
|
@ -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;
|
||||||
|
};
|
||||||
|
|
||||||
|
})();
|
|
@ -39,13 +39,6 @@ var Engine = {};
|
||||||
positionIterations: 6,
|
positionIterations: 6,
|
||||||
velocityIterations: 4,
|
velocityIterations: 4,
|
||||||
constraintIterations: 1,
|
constraintIterations: 1,
|
||||||
pairs: {
|
|
||||||
table: {},
|
|
||||||
list: [],
|
|
||||||
collisionStart: [],
|
|
||||||
collisionActive: [],
|
|
||||||
collisionEnd: []
|
|
||||||
},
|
|
||||||
enableSleeping: false,
|
enableSleeping: false,
|
||||||
timeScale: 1,
|
timeScale: 1,
|
||||||
input: {},
|
input: {},
|
||||||
|
@ -68,6 +61,7 @@ var Engine = {};
|
||||||
|
|
||||||
engine.render = engine.render.controller.create(engine.render);
|
engine.render = engine.render.controller.create(engine.render);
|
||||||
engine.world = World.create(engine.world);
|
engine.world = World.create(engine.world);
|
||||||
|
engine.pairs = Pairs.create();
|
||||||
engine.metrics = engine.metrics || Metrics.create();
|
engine.metrics = engine.metrics || Metrics.create();
|
||||||
engine.input.mouse = engine.input.mouse || Mouse.create(engine.render.canvas);
|
engine.input.mouse = engine.input.mouse || Mouse.create(engine.render.canvas);
|
||||||
engine.mouseConstraint = engine.mouseConstraint || MouseConstraint.create(engine.input.mouse);
|
engine.mouseConstraint = engine.mouseConstraint || MouseConstraint.create(engine.input.mouse);
|
||||||
|
@ -273,8 +267,8 @@ var Engine = {};
|
||||||
// update pairs
|
// update pairs
|
||||||
var pairs = engine.pairs,
|
var pairs = engine.pairs,
|
||||||
timestamp = engine.timing.timestamp;
|
timestamp = engine.timing.timestamp;
|
||||||
Manager.updatePairs(pairs, collisions, timestamp);
|
Pairs.update(pairs, collisions, timestamp);
|
||||||
Manager.removeOldPairs(pairs, timestamp);
|
Pairs.removeOld(pairs, timestamp);
|
||||||
|
|
||||||
// wake up bodies involved in collisions
|
// wake up bodies involved in collisions
|
||||||
if (engine.enableSleeping)
|
if (engine.enableSleeping)
|
||||||
|
@ -340,8 +334,7 @@ var Engine = {};
|
||||||
Engine.clear = function(engine) {
|
Engine.clear = function(engine) {
|
||||||
var world = engine.world;
|
var world = engine.world;
|
||||||
|
|
||||||
engine.pairs.table = {};
|
Pairs.clear(engine.pairs);
|
||||||
engine.pairs.list = [];
|
|
||||||
|
|
||||||
World.addConstraint(engine.world, engine.mouseConstraint.constraint);
|
World.addConstraint(engine.world, engine.mouseConstraint.constraint);
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ Matter.World = World;
|
||||||
Matter.Contact = Contact;
|
Matter.Contact = Contact;
|
||||||
Matter.Detector = Detector;
|
Matter.Detector = Detector;
|
||||||
Matter.Grid = Grid;
|
Matter.Grid = Grid;
|
||||||
Matter.Manager = Manager;
|
Matter.Pairs = Pairs;
|
||||||
Matter.Pair = Pair;
|
Matter.Pair = Pair;
|
||||||
Matter.Resolver = Resolver;
|
Matter.Resolver = Resolver;
|
||||||
Matter.SAT = SAT;
|
Matter.SAT = SAT;
|
||||||
|
|
Loading…
Reference in a new issue