嗨,我正在尝试制作一个简单的乒乓球游戏,我遇到了碰撞检测的麻烦。球没有在球拍中注册。
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;
}
答案 0 :(得分: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 {
这可以通过将球放在“假想的盒子”内部来实现,当“假想盒子”的任何边缘接触到矩形的任何边缘时,就会检测到碰撞。