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