示例#1
0
        private bool BallPaddleCollision(Ball ball, Paddle paddle)
        {
            Vector2 circleDistance = new Vector2(Math.Abs(ball.GetPosition().x - paddle.GetRect().X), Math.Abs(ball.GetPosition().y - paddle.GetRect().Y));

            if (circleDistance.x > (paddle.GetRect().Width / 2.0 + ball.GetRadius()))
            {
                return(false);
            }
            if (circleDistance.y > (paddle.GetRect().Height / 2.0 + ball.GetRadius()))
            {
                return(false);
            }

            if (circleDistance.x <= paddle.GetRect().Width / 2.0)
            {
                return(true);
            }
            if (circleDistance.y <= paddle.GetRect().Height / 2.0)
            {
                return(true);
            }

            double cornerDistance = Math.Pow(circleDistance.x - paddle.GetRect().Width / 2.0, 2) + Math.Pow(circleDistance.y - paddle.GetRect().Height / 2.0, 2);

            return(cornerDistance <= (Math.Pow(ball.GetRadius(), 2)));
        }