/// <summary> /// Проверка на столкновение мяча с прямоугольным объектом /// </summary> /// <param name="ball"></param> /// <param name="rect"></param> /// <returns></returns> private bool CheckRect(Ball ball, AbstractRectangle rect) { var position = new Point(); position.X = ball.Position.X + ball.Size.Width; position.Y = ball.Position.Y + ball.Size.Height; var rectangle = new Rect(rect.Position, rect.Size); return(rectangle.Contains(position)); }
/// <summary> /// Проверка характера столкновения мяча с прямоугольным объектом /// </summary> /// <param name="ball"></param> /// <param name="rect"></param> /// <returns></returns> private void HandleCollision(Ball ball, AbstractRectangle rect) { var speed = ball.Speed; speed.X = GetBallSpeedX(ball, rect); speed.Y *= -1; ball.Speed = speed; if (rect is Platform) { ChangeSpeed(ball, rect as Platform); } }
/// <summary> /// Получение скорости мяча по Х /// </summary> /// <param name="ball"></param> /// <param name="rect"></param> /// <returns></returns> private double GetBallSpeedX(Ball ball, AbstractRectangle rect) { var ballX1 = ball.Position.X; var ballX2 = ball.Position.X + ball.Size.Width; var ballX0 = (ballX1 + ballX2) / 2; //Мяч ударился об угол о______ || _______о if (!(ballX0 >= rect.Position.X && ballX0 <= rect.Position.X + rect.Size.Width)) { return(ball.Speed.X * (-1)); } // __o____ || _______ if (ballX0 >= rect.Position.X && ballX0 <= rect.Position.X + rect.Size.Width / 3) { return(Math.Abs(ball.Speed.X) * (-1) - 0.2); } // ______ || _____o__ if (ballX0 >= rect.Position.X + rect.Size.Width / 3 * 2 && ballX0 <= rect.Position.X + rect.Size.Width) { return(Math.Abs(ball.Speed.X) + 0.2); } return(ball.Speed.X); }