简体   繁体   中英

collision detection not registering with ball

Hi I'm trying to make a simple pong game and I'm running into trouble with the collision detection. The ball is not registering with the paddle.

    function moveBall() {
    var rightRadius = ballX + radius;
    var leftRadius = ballX -radius;

    if (ballX + radius > canvas.width || ballX - radius < 0) {
        ballOffX = -ballOffX;
    }
    /*
    The following code is handling the collision of the ball with the plate
     */
   if((rightRadius <= (player1.x + paddleWidth))&&(leftRadius >= player1.x) &&(player1.y == ballY + 10)){
        ballOffY = -ballOffY;
    }

    ballX += ballOffX;
    ballY += ballOffY;

}

在此处输入图片说明

I made an if statement to sense collisions in JavaScript, here it is:

if circle x < rect x + circle width && circle x + rect width > rect x && circle y < rect y + circle height && rect height + circle y > rect y {

this works by putting the ball inside of an 'imaginary box' and when any of the edges of the 'imaginary box' touch any of the edges of the rectangle, the collision is detected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM