本文介绍了Java - 乒乓球拍和球碰撞错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打乒乓球,我已经将球的 x 坐标设置为在它击中球拍时立即反转,并在没有击中球拍时停止.这段代码在大部分"时间都有效,但有时"球会在没有明显原因的情况下击中桨后立即停止.我可以获得的任何提示或提示.附言我找不到有同样问题的人

I'm making pong and I have set the balls x cordinate to reverse as soon as it hits the paddle and stop when it doesnt hit the paddle. this code works "most" of the time, but "sometimes" the ball just stops as soon as it hits the paddle for no apparent reason. any tips or hints i could get for this. p.s. i couldn't find anyone with the same problem

这是代码段:

        //ball bounces on p1's paddle
    if(nextBallLeft < p1RightSide){
        if(ballY > p1Y && ballY < getHeight() - p1Y + paddleHeight){
             ballDeltaX *= -1;
        }
        else{
            System.out.println("1");
            ballDeltaX = 0;
            ballDeltaY= 0;

        }

推荐答案

有一种情况,你的球会进入"桨,碰撞会使它反转.在下一个循环中,当它仍在桨内时,它将再次反转.这种模式会一直持续下去,你的球会卡住.

There's a case where your ball will "enter" the paddle, and the collision will make it reverse. In the next loop, while it's still inside the paddle, it will reverse again. This pattern goes on forever and your ball will get stuck.

要解决此问题,您需要跟踪"球路径并在撞墙之前检测碰撞.这样,您就可以使球的下一步成为到达球拍表面的必要量.

To solve this you need to 'trace' the ball path and detect collision before hitting the wall. This way you can make the next step of the ball be the necessary amount to reach the surface of your paddle.

这篇关于Java - 乒乓球拍和球碰撞错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:35