Skip to content

Commit

Permalink
Updated collision detection so it doesn't scan all tiles, instead it …
Browse files Browse the repository at this point in the history
…only scans the tiles adjacent to the player (9 max, depending on how the player intersects)
  • Loading branch information
jagregory committed Jun 7, 2011
1 parent 8e424be commit ccdc741
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions public/js/game/kaboom.game.js
Expand Up @@ -16,6 +16,7 @@ var KaboomGame = function (levelMap) {
this.players = [];
this.DISTANCE = 5;
this.TILE_SIZE = 48;
this.COLLISION_RANGE = 1;
};

KaboomGame.prototype = {
Expand Down Expand Up @@ -84,29 +85,38 @@ KaboomGame.prototype = {
return new Position(tileX, tileY);
},


update: function() {
var game = this;
var tryMove = function(pos, delta) {
var newPos = pos.translate(delta);
var chosen = newPos;
var gridPos = {
topLeft: game.pixelsToTiles(newPos.topLeft),
bottomRight: game.pixelsToTiles(newPos.bottomRight)
};
var collisionRange = {
xStart: Math.max(gridPos.topLeft.x - game.COLLISION_RANGE, 0),
xEnd: Math.min(gridPos.bottomRight.x + game.COLLISION_RANGE, 16), // TODO: un-hardcode this
yStart: Math.max(gridPos.topLeft.y - game.COLLISION_RANGE, 0),
yEnd: Math.min(gridPos.bottomRight.y + game.COLLISION_RANGE, 12) // TODO: un-hardcode this
};

game.level.forEachTile(function(tile) {
var bounds = tile.getBounds(game);

tile.candidate = false;
tile.isIntersecting = false;

if (bounds.intersects(newPos.contract(4))) {
if (tile.solid) {
for (var x = collisionRange.xStart; x <= collisionRange.xEnd; x++) {
for (var y = collisionRange.yStart; y <= collisionRange.yEnd; y++) {
var tile = game.level.rows[y][x];
var bounds = tile.getBounds(game);
tile.isIntersecting = false;

if (bounds.intersects(newPos.contract(4))) {
tile.isIntersecting = true;
chosen = pos;
return;
if (tile.solid) {
return pos;
}
}
}
});
}

return chosen;
return newPos;
};

this.players.forEach(function(p, idx) {
Expand Down

0 comments on commit ccdc741

Please sign in to comment.