SpriteBatch sb; //The sprite batch, used to draw things. #endregion Fields #region Constructors /// <summary> /// Constructor. /// </summary> /// <param name="sb">The sprite batch</param> /// <param name="c">The content manager</param> public GameState(SpriteBatch sb, ContentManager c) { GameTime time = new GameTime(); controller = new Controller(); doPack = new doPacket(this, time, controller); drawPack = new drawPacket(this, time, sb); content = c; gameWorld = new RenderTarget2D(sb.GraphicsDevice, 1280, 720); effects = new RenderTarget2D(sb.GraphicsDevice, 1280, 720); UI = new RenderTarget2D(sb.GraphicsDevice, 1280, 720); pauseBG = new RenderTarget2D(sb.GraphicsDevice, 1280, 720); exitGame = false; playerPosition = new CollisionPoint(); cameraPosition = new Point(); paused = false; frameCaptured = false; }
/// <summary> /// This method primarily calls the collision detection from Wall. Then, if there is a collision, it will move the player with it. /// </summary> /// <param name="positions">A list containing every point an object intends to check for collisions. This parameter is primarily used to perform different logic on different points. /// For instance, a floor may only need to check the bottom points, and for all other points, may skip any logic at all.</param> /// <param name="pos">The collision point that is currently being tested for collision.</param> /// <param name="trajectory">The velocity vector which represent's where the current point is going.</param> /// <param name="nearby">A sphere of variable radius which represents how close an object needs to be to be considered possible for collision. /// If the block's hit box does not intersect this sphere, no logic should be applied.</param> /// <param name="player">The player against whom the collision is being tested.</param> /// <returns>A final velocity. If there is no collision, this will return trajectory. Otherwise, it will return a velocity vector appropriate to the collision which took place.</returns> public override Vector2 detectCollision(List<CollisionPoint> positions, CollisionPoint pos, Vector2 trajectory, BoundingSphere nearby, Player player) { postCollision = base.detectCollision(positions, pos, trajectory, nearby, player); //Use the collision detection from Wall. if (postCollision.X != trajectory.X || postCollision.Y != trajectory.Y && !trajectorySet) //IF: A collision was detected { if (dimensions.Min.X < pos.X && pos.X < dimensions.Max.X) //IF: pos.x is within the X dimensions of the hitbox { playerSpeed.X = curSpeed.X; //Move the player playerSpeed.Y = curSpeed.Y; player.movePlayer(playerSpeed); player.onGround(); trajectorySet = true; //We have now moved the player } } if (pos == positions[11]) //IF: We are done detecting collision trajectorySet = false; //Reset the switch return postCollision; }
/// <summary> /// This method primarily calls the collision detection of wall, but if a collision is detected, it will also initiate the object's disappearance. /// </summary> /// <param name="positions">A list containing every point an object intends to check for collisions. This parameter is primarily used to perform different logic on different points. /// For instance, a floor may only need to check the bottom points, and for all other points, may skip any logic at all.</param> /// <param name="pos">The collision point that is currently being tested for collision.</param> /// <param name="trajectory">The velocity vector which represent's where the current point is going.</param> /// <param name="nearby">A sphere of variable radius which represents how close an object needs to be to be considered possible for collision. /// If the block's hit box does not intersect this sphere, no logic should be applied.</param> /// <param name="player">The player against whom the collision is being tested.</param> /// <returns>A final velocity. If there is no collision, this will return trajectory. Otherwise, it will return a velocity vector appropriate to the collision which took place.</returns> public override Vector2 detectCollision(List<CollisionPoint> positions, CollisionPoint pos, Vector2 trajectory, BoundingSphere nearby, Player player) { postCollision = base.detectCollision(positions, pos, trajectory, nearby, player); if (postCollision.X != trajectory.X || postCollision.Y != trajectory.Y) //IF: trajectory is different than the return velocity (IE, collision has happened) disappearing = true; //Initiate disappearance return postCollision; //Pass the final velocity along }
/// <summary> /// This method contains collision detection logic. It will process the horizontal movement and then find the appropriate vertical position. /// </summary> /// <param name="positions">A list containing every point an object intends to check for collisions. This parameter is primarily used to perform different logic on different points. /// For instance, a floor may only need to check the bottom points, and for all other points, may skip any logic at all.</param> /// <param name="pos">The collision point that is currently being tested for collision.</param> /// <param name="trajectory">The velocity vector which represent's where the current point is going.</param> /// <param name="nearby">A sphere of variable radius which represents how close an object needs to be to be considered possible for collision. /// If the block's hit box does not intersect this sphere, no logic should be applied.</param> /// <param name="player">The player against whom the collision is being tested.</param> /// <returns>A final velocity. If there is no collision, this will return trajectory. Otherwise, it will return a velocity vector appropriate to the collision which took place.</returns> public override Vector2 detectCollision(List<CollisionPoint> positions, CollisionPoint pos, Vector2 trajectory, BoundingSphere nearby, Player player) { if (dimensions.Intersects(nearby)) //If it is nearby { Boolean ascendingRight = false; //Is this slope ascending right? if (slope < 0) ascendingRight = true; if ((ascendingRight && pos == positions[6]) || //Only process the bottom right corner if the slope ascends right, otherwise only process the bottom left corner. (!ascendingRight && pos == positions[9])) { if ((pos.Y <= 5 + ((slope * pos.X) + intercept) && //IF: pos crosses the slope's line, with a 5 pixel leeway. (Without leeway, we have a weird physics effect, ((pos.Y + trajectory.Y) > (slope * (pos.X + trajectory.X)) + intercept))) //where the slope would launch the player into the air.) { //Collision is detected. if (trajectory.X < 1) //If the trajectory is close to vertical... { intersect = pos.X; //..then use a vertical trajectory. } else { m = trajectory.Y / trajectory.X; //Find the standard form equation of the incoming trajectory. b = pos.Y - (m * pos.X); intersect = (intercept - b) / (m - slope); //Find the intersect of the two lines. } if (dimensions.Min.X <= intersect && intersect <= dimensions.Max.X) //If the collision is detected within the horizontal bounds of the slope { postCollision.X = trajectory.X; //Set postcollision postCollision.Y = (slope * (pos.X + postCollision.X)) + intercept; postCollision.Y -= pos.Y; postCollision.Y--; player.onGround(); //Player has landed return postCollision; } } } else if ((ascendingRight && pos == positions[0]) || (!ascendingRight && pos == positions[3])) { if((pos.Y >= height + ((slope * pos.X) + intercept) - 5) && ((pos.Y + trajectory.Y) < (slope * (pos.X + trajectory.X)) + intercept + height)) { if (dimensions.Min.X <= pos.X && pos.X <= dimensions.Max.X) { postCollision.X = 0; postCollision.Y = 2; return postCollision; } } } } return trajectory; }