2016-07-25 48 views
0

嗨我想做一個簡單的pong遊戲,我遇到了碰撞檢測的麻煩。球沒有登記槳。碰撞檢測不註冊球

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; 

} 

enter image description here

+0

球員球盤?你能展示一個示例圖片嗎? –

+0

我重命名板,希望這有助於。謝謝 – Muffin

+0

您應該使用矢量來檢測碰撞。 – kollein

回答

0

我做了一個if語句來檢測在JavaScript中的碰撞,那就是:

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 { 

這部作品通過把球內的「虛構的盒子」,並在任何的'虛擬盒子'的邊緣碰到矩形的任何邊緣,就會檢測到碰撞。