public static void BallBrickCollision(Ball ball, Vector2 colPoint, Brick brick) { float ballMiddle = (ball.X + (ball.Width / 2)); if (colPoint.Y >= (brick.Y + brick.Height - 1) || (colPoint.Y <= (brick.Y) && (ballMiddle > brick.X || ballMiddle < (brick.X + brick.Width)))) { ball.ChangeVertDirection(); } else if (ball.MidY > (brick.Y + brick.Height) && ball.SpeedY < 0) { ball.ChangeVertDirection(); ball.ChangeHorzDirection(); } else { ball.ChangeHorzDirection(); } }
public static bool CheckBallScreenCollision(Ball ball, ref float delayTimer) { int maxX = Game1.graphics.GraphicsDevice.Viewport.Width - ball.Width; int maxY = Game1.graphics.GraphicsDevice.Viewport.Height - ball.Height; // Check for bounce. Make sure to place ball back inside the screen // or it could remain outside the screen on the next iteration and cause // a back-and-forth bouncing logic error. if (ball.X > maxX) { ball.ChangeHorzDirection(); ball.X = maxX; return(false); } else if (ball.X < 0) { ball.ChangeHorzDirection(); ball.X = 0; return(false); } if (ball.Y < 0) { ball.ChangeVertDirection(); ball.Y = 0; return(false); } else if (ball.Y > maxY) { // Game over - reset ball Game1.crashSound.Play(); ball.Reset(); // Reset timer and stop ball's Update() from executing delayTimer = 0; ball.Enabled = false; return(true); } return(false); }
public static void BallPaddleCollision(Ball ball, Paddle paddle) { // If hitting the side of the paddle the ball is coming toward, // switch the ball's horz direction float ballMiddle = (ball.X + (ball.Width / 2)); float paddleMiddle = (paddle.X + paddle.Width) / 2; if ((ballMiddle < paddle.X && ball.SpeedX > 0) || (ballMiddle > (paddle.X + paddle.Width) && ball.SpeedX < 0)) { ball.ChangeHorzDirection(); } // Go back up the screen and speed up ball.ChangeVertDirection(); ball.SpeedUp(); }