public void Update(GameTime gameTime) { //previousPosition = position; float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; position += velocity * elapsed; //tracer = new Ray(new Vector3(previousPosition, 0), new Vector3(position - previousPosition, 0)); //Wall Detection // Get the player's bounding rectangle and find neighboring tiles. Rectangle bounds = BoundingRectangle; int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width); int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // Reset flag to search for ground collision. //isOnGround = false; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { // If this tile is collidable, TileType collision = game.map.GetCollision(x, y); if (collision != TileType.Ground) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = game.map.GetBounds(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds); if (depth != Vector2.Zero) { this.isStopped = true; } } } } for (int i = game.hoard.hoard.Count - 1; i >= 0; i--) { Enemy enemy = game.hoard.hoard[i]; Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, enemy.BoundingRectangle); if (depth != Vector2.Zero) { enemy.DoDamage(damage); if (!enemy.isAlive) { game.hoard.hoard.Remove(enemy); } this.isStopped = true; } } }
private void HandleCollisions() { // Get the player's bounding rectangle and find neighboring tiles. Rectangle bounds = BoundingRectangle; int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width); int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { // If this tile is collidable, TileType collision = game.map.GetCollision(x, y); if (collision != TileType.Ground) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = game.map.GetBounds(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds); if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); // Resolve the collision along the shallow axis. if (collision == TileType.Wall) { if (absDepthY < absDepthX) { // Resolve the collision along the Y axis. Position = new Vector2(Position.X, Position.Y + depth.Y); velocity.Y += depth.Y; mousePosition.Y += depth.Y; } if (absDepthX < absDepthY) { Position = new Vector2(Position.X + depth.X, Position.Y); velocity.X += depth.X; mousePosition.X += depth.X; } // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } } } } }
private void HandleCollisions() { // Get the player's bounding rectangle and find neighboring tiles. Rectangle bounds = BoundingRectangle; int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width); int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // Reset flag to search for ground collision. //isOnGround = false; // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { // If this tile is collidable, TileType collision = map.GetCollision(x, y); if (collision != TileType.Ground) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = Map.GetBounds(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds); if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); if (collision == TileType.Wall) { // Y Axis Collision if (absDepthY < absDepthX) { position = new Vector2(Position.X, Position.Y + depth.Y); if (velocity.X > 0 && absDepthX < 44) { position.X += absDepthY; } else if (velocity.X < 0 && absDepthX < 44) { position.X -= absDepthY; } } //X Axis Collision if (absDepthX < absDepthY) { position = new Vector2(Position.X + depth.X, Position.Y); if (velocity.Y > 0 && absDepthY < 44) { position.Y += absDepthX; } else if (velocity.Y < 0 && absDepthY < 44) { position.Y -= absDepthX; } } // Perform further collisions with the new bounds. bounds = BoundingRectangle; } //else if (collision == TileType.Wall) // Ignore platforms. //{ // // Resolve the collision along the X axis. // Position = new Vector2(Position.X + depth.X, Position.Y); // // Perform further collisions with the new bounds. // bounds = BoundingRectangle; //} } } } } // Save the new bounds bottom. //previousBottom = bounds.Bottom; }