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:
parent
471754611a
commit
9a2511dd50
1 changed files with 21 additions and 20 deletions
|
@ -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);
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
// 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 _createActivePairsList to work
|
||||||
var pairId = Pair.id(body, bodyB);
|
var bodyB = bucket[i],
|
||||||
if (grid.pairs[pairId]) {
|
pairId = Pair.id(body, bodyB),
|
||||||
var pairCount = grid.pairs[pairId][2];
|
pair = grid.pairs[pairId];
|
||||||
grid.pairs[pairId][2] = pairCount > 0 ? pairCount - 1 : 0;
|
|
||||||
}
|
if (pair)
|
||||||
}
|
pair[2] -= 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue