//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnTopBorder // // Description: // Check if the call collides with the top side of the border and reverse the Y velocity if so. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* private void CheckBallCollisionOnTopBorder() { // Reverse the ball Y velocity if the top border is hit by the ball. if (mHitBox.Y < 0) { Vector.ReverseComponentY(); } }
//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnPaddle // // Description: // Determines if the ball has collided with the paddle. If the center of the ball is within the width of the paddle then ball will reverse // in its Y velocity. Otherwise the ball hit the side or corner of the paddle and the X and Y velocity are reversed. // // Arguments: // theBreakoutGame - The game object that tracks various game elements. // // Return: // N/A // //********************************************************************************************************************************************* public void CheckBallCollisionOnPaddle(BreakoutGame theBreakoutGame) { // Check if the ball hits the paddle. if (mHitBox.IntersectsWith(theBreakoutGame.Paddle.HitBox)) { // Keep the ball above the paddle to avoid the ball getting stuck inside the paddle. mHitBox.Y = theBreakoutGame.Paddle.HitBox.Y - mHitBox.Height; mInnerHitDetection.Y = mHitBox.Y + mInnerHitPadding; // The Y (vertical) component is always reversed on a paddle hit. Vector.ReverseComponentY(); // Transfer a percentage of the paddles velocity to the ball. This helps the player to change to angle of the ball with the paddle. Vector.SetComponentX(Vector.ComponentX + (theBreakoutGame.Paddle.Vector.ComponentX * BreakoutConstants.PADDLE_VECTOR_TRANSFER_PERCENTAGE)); } }
//********************************************************************************************************************************************* // // Method Name: CheckRectangleEdgeCollision // // Description: // Determines which edge the ball collided with against the brick. Collision with the top or bottom will reverse the balls y velocity, // collision with the left or right edge will reverse the balls x velocity, and a corner collision will reverse both the balls x and y // velocity. // // Arguments: // theBrick - The brick the ball collided with. // // Return: // N/A // //********************************************************************************************************************************************* private void CheckRectangleEdgeCollision(Brick theBrick) { // Check if the center of the ball is between the width of the brick. If so, then the ball hit the bottom or top side of the brick. // The ball's Y velocity is reversed in this case. if ((mInnerHitDetection.X + mInnerHitDetection.Width) > theBrick.HitBox.X && mInnerHitDetection.X < (theBrick.HitBox.X + theBrick.HitBox.Width)) { Vector.ReverseComponentY(); } // Check if the center of the ball is between the height of the brick. If so, then the ball hit the left or right side of the brick. // The ball's X velocity is reversed in this case. else if ((mInnerHitDetection.Y + mInnerHitDetection.Height) > theBrick.HitBox.Y && mInnerHitDetection.Y < (theBrick.HitBox.Y + theBrick.HitBox.Height)) { Vector.ReverseComponentX(); } // Otherwise the ball hit the corner of the brick. // The ball's x and y velocity are reversed in this case. else { Vector.ReverseComponentX(); Vector.ReverseComponentY(); } }