Пример #1
0
 private void SetCollisionY(Ball ball)
 {
     // Ball move towards bottom right
     if (ball.Velocity.X > 0 && ball.Velocity.Y > 0) {
         ball.VelocityY = ball.Velocity.Y * -1;
     }
     // Ball move towards top right
     else if (ball.Velocity.X > 0 && ball.Velocity.Y < 0) {
         ball.VelocityY = Math.Abs(ball.Velocity.Y);
     }
     // Ball move towards top left
     else if (ball.Velocity.X < 0 && ball.Velocity.Y < 0) {
         ball.VelocityY = Math.Abs(ball.Velocity.Y);
     }
     // Ball move towards bottom left
     else if (ball.Velocity.X < 0 && ball.Velocity.Y > 0) {
         ball.VelocityY = ball.Velocity.Y * -1;
     }
 }
Пример #2
0
        private void CheckBoxCollision(Ball ball)
        {
            // Ball hit top
            if (ball.Position.Y - ball.Radius <= 0.0f) {
                this.SetCollisionY (ball);
                ball.PositionY = 0.0f + ball.Radius;
            }

            // Ball hit bottom
            else if (ball.Position.Y + ball.Radius >= 1.0f) {
                this.SetCollisionY (ball);
                ball.PositionY = 1.0f - ball.Radius;
            }

            // Ball hit left
            else if (ball.Position.X - ball.Radius <= 0.0f) {
                this.SetCollisionX (ball);
                ball.PositionX = 0.0f + ball.Radius;
            }

            // Ball hit right
            else if (ball.Position.X + ball.Radius >= 1.0f) {
                this.SetCollisionX (ball);
                ball.PositionX = 1.0f - ball.Radius;
            }
        }