//********************************************************************************************************************************************* // // Method Name: CheckBallCollisionOnSideBorders // // Description: // Check if the call collides with the left or right side of the borders and reverse the X velocity if so. // // Arguments: // N/A // // Return: // N/A // //********************************************************************************************************************************************* private void CheckBallCollisionOnSideBorders() { // Reverse the ball x velocity if the left or right border is hit. if (mHitBox.X < 0 || mHitBox.X > (BreakoutConstants.SCREEN_PLAY_AREA_WIDTH - mHitBox.Width)) { Vector.ReverseComponentX(); } }
//********************************************************************************************************************************************* // // 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(); } }