0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

fixed a leak in grid broadphase

This commit is contained in:
liabru 2014-03-22 14:44:56 +00:00
parent 471754611a
commit 9a2511dd50

View file

@ -220,9 +220,11 @@ var Grid = {};
// keep track of the number of buckets the pair exists in
// important for Grid.update to work
var pairId = Pair.id(body, bodyB);
if (grid.pairs[pairId]) {
grid.pairs[pairId][2] += 1;
var pairId = Pair.id(body, bodyB),
pair = grid.pairs[pairId];
if (pair) {
pair[2] += 1;
} else {
grid.pairs[pairId] = [body, bodyB, 1];
}
@ -241,23 +243,19 @@ var Grid = {};
* @param {} body
*/
var _bucketRemoveBody = function(grid, bucket, body) {
// remove from bodies
for (var i = 0; i < bucket.length; i++) {
var bodyB = bucket[i];
// remove from bucket
bucket.splice(bucket.indexOf(body), 1);
// when position of body in bucket array is found, remove it
if (bodyB.id === body.id) {
bucket.splice(i, 1);
continue;
} else {
// update pair counts
for (var i = 0; i < bucket.length; i++) {
// keep track of the number of buckets the pair exists in
// important for Grid.update to work
var pairId = Pair.id(body, bodyB);
if (grid.pairs[pairId]) {
var pairCount = grid.pairs[pairId][2];
grid.pairs[pairId][2] = pairCount > 0 ? pairCount - 1 : 0;
}
}
// important for _createActivePairsList to work
var bodyB = bucket[i],
pairId = Pair.id(body, bodyB),
pair = grid.pairs[pairId];
if (pair)
pair[2] -= 1;
}
};
@ -282,8 +280,11 @@ var Grid = {};
// if pair exists in at least one bucket
// it is a pair that needs further collision testing so push it
if (pair[2] > 0)
if (pair[2] > 0) {
pairs.push(pair);
} else {
delete grid.pairs[pairKeys[k]];
}
}
return pairs;