My collision detection does not work whatsoever

  javascript, pong

So I’m making a Pong game for a programming exercise. Everything else seems to work, but the collision detection does not work whatsoever as the ball flies through the paddles like a phantom.


    player.top = player.y;
    player.right = player.x + player.width; 
    player.bottom = player.y + player.height; 
    player.left = player.x;

    ball.top = ball.y - ball.radius; 
    ball.right = ball.x + ball.radius; 
    ball.bottom = ball.y + ball.radius; 
    ball.left - ball.x - ball.radius; 

    return ball.left < player.right && ball.top < player.bottom && ball.right > player.left && ball.bottom > player.top;

}
let player = (ball.x < canvas.width / 2) ? playerOne : playerTwo; // where i'm calling the collision detection function in update
    
    if (collisionDetection(player, ball)){ 

        let angle = 0; 

        if (ball.y < (player.y + player.height / 2)){
            angle = -1 * Math.PI / 4;
        }

        else if (ball.y > (player.y + player.height / 2)){ 
            angle = Math.PI / 4;
        }

        ball.velocityX = (player === playerOne ? 1 : -1) * ball.speed * Math.cos(angle);
        ball.velocityY = ball.speed * Math.sin(angle);

        ball.speed += 0.22;

    }

I’m not sure whether it’s the math that I’m doing wrong, or if I’m using the player variables incorrectly, but either way it refuses to work.

Source: Ask Javascript Questions

LEAVE A COMMENT