private void checkVerticalCollision(Projectile p, int pxDown) { int forwardEdge = (int)p.Position.Y; //1 for down, -1 for up int yDirection = pxDown / Math.Abs(pxDown); //start at initial forward edge int startRow = forwardEdge / _collisionLayer.TileHeight; //assume unit moves full distance p.Position.Y += pxDown; //check up to new assumed unit hitrect edge int endRow = (forwardEdge + pxDown) / _collisionLayer.TileHeight; startRow = (int)MathHelper.Clamp(startRow, 0, _collisionLayer.LayerHeight - 1); endRow = (int)MathHelper.Clamp(endRow, 0, _collisionLayer.LayerHeight - 1); for (int row = startRow; row != (endRow + yDirection); row = row + yDirection) { for (int col = ((int)p.Position.X + 1) / _collisionLayer.TileWidth; col <= (p.Position.X - 1) / _collisionLayer.TileWidth; col++) { //boundary checks---------------------------------------------------------------- if (col < 0) { p.CollideWithObstacle(Direction.West); p.Position.X = 0; break; } else if (col > _collisionLayer.LayerWidth) { p.CollideWithObstacle(Direction.East); p.Position.Y = _collisionLayer.LayerWidth * _collisionLayer.TileWidth; break; } if (row < 0) { p.CollideWithObstacle(Direction.North); p.Position.Y = 0; break; } else if (row > _collisionLayer.LayerHeight) { p.CollideWithObstacle(Direction.South); p.Position.Y = _collisionLayer.LayerHeight * _collisionLayer.TileHeight; break; } //-------------------------------------------------------------------------------- if (_collisionLayer.Tiles[col, row] != null && _collisionLayer.Tiles[col, row].TileIndex != 0) { if (pxDown > 0) { p.CollideWithObstacle(Direction.South); p.Position.Y = row * _collisionLayer.TileHeight; } else { p.CollideWithObstacle(Direction.North); p.Position.Y = (row + 1) * _collisionLayer.TileHeight; } break; //no need to check more } } } }
private void checkHorizontalCollision(Projectile p, int pxRight) { //Get the coordinate of the forward-facing edge //If walking left, forwardEdge = left of bounding box //If walking right, forwardEdge = right of bounding box int forwardEdge = (int)p.Position.X; //-1 for left, 1 for right int xDirection = pxRight / Math.Abs(pxRight); int startCol = forwardEdge / _collisionLayer.TileWidth; int endCol = (forwardEdge + pxRight) / _collisionLayer.TileWidth; startCol = (int)MathHelper.Clamp(startCol, 0, _collisionLayer.LayerWidth - 1); endCol = (int)MathHelper.Clamp(endCol, 0, _collisionLayer.LayerWidth - 1); p.Position.X += pxRight; for (int col = startCol; col != (endCol + xDirection); col = col + xDirection) { for (int row = (int)p.Position.Y / _collisionLayer.TileHeight; row <= p.Position.Y / _collisionLayer.TileHeight; row++) { if (!_collisionLayer.IsValidTileLocation(col, row)) { continue; } //within bounds -- check tile collision else if (_collisionLayer.Tiles[col, row] != null && _collisionLayer.Tiles[col, row].TileIndex != 0) { if (pxRight > 0) { p.CollideWithObstacle(Direction.East); p.Position.X = col * _collisionLayer.TileWidth; } else { p.CollideWithObstacle(Direction.West); p.Position.X = (col + 1) * _collisionLayer.TileWidth; } break; //no need to check more } } } }