Java突破游戏弹跳球

Java突破游戏弹跳球,java,2d,game-engine,breakout,Java,2d,Game Engine,Breakout,我想用java做一个突破游戏。球碰到石头和球拍时会反弹,但碰到墙壁后不会反弹。看看我用的是什么 谁能发现代码中的错误 谢谢 编辑:当打印速度时,我得到这个输出 vx 0.0 vy 0.0 vx 0.02 vy -0.02 vx 0.02 vy -0.02 vx 0.02 vy -0.02 vx 0.02 vy -0.02 vx 0.02 vy -0.02 vx 0.02 vy -0.02 vx 0.02 vy -0.02 vx 0.02

我想用java做一个突破游戏。球碰到石头和球拍时会反弹,但碰到墙壁后不会反弹。看看我用的是什么

谁能发现代码中的错误

谢谢

编辑:当打印速度时,我得到这个输出

vx 0.0     vy  0.0
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.0     vy  0.0
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
vx 0.02    vy -0.02
编辑 这就是解决办法。我已将bounce()方法更改为此

/**
 * This object bounces back from the other object in a natural way. Please
 * realize that the bounce is not completely accurate because this depends
 * on many properties. But in many situations the effect is good enough. Had
 * some bugs in pixel perfect detection mode if the image has a larger area
 * of complete alpha. If using PPCD, make the object fit the image size by
 * removing the alpha and resizing the image.
 */
public void bounce(GObject other){
    int xd = (int) ((other.x + other.getWidth() / 2) - (x + getWidth() / 2));
    int yd = (int) ((other.y + other.getHeight() / 2) - (y + getHeight() / 2));
    if (xd < 0) {
        xd = -xd;
    }
    if (yd < 0) {
        yd = -yd;
    }
    if (xd > yd) {
        dx = -dx;
    } else {
        dy = -dy;
    }
}
/**
*此对象以自然方式从另一个对象反弹回来。请
*请意识到反弹并非完全准确,因为这取决于
*在许多属性上。但在许多情况下,效果已经足够好了。有
*如果图像面积较大,像素完美检测模式中会出现一些错误
*完全阿尔法。如果使用PPCD,请通过以下方式使对象适合图像大小
*移除alpha并调整图像大小。
*/
公共无效反弹(其他){
intxd=(int)((other.x+other.getWidth()/2)-(x+getWidth()/2));
int yd=(int)((other.y+other.getHeight()/2)-(y+getHeight()/2));
if(xd<0){
xd=-xd;
}
if(yd<0){
yd=-yd;
}
如果(xd>yd){
dx=-dx;
}否则{
dy=-dy;
}
}

此问题的一个常见原因是碰撞对象重叠,并卡在连续碰撞的状态中

因此,球的移动速度相对较快,并与一个挡块碰撞。然而,由于模拟是离散的,球实际上会稍微进入块中。然后正确检测碰撞并反转速度。但是,在下一个更新周期中,无论出于何种原因,球可能仍在块内。因此,程序检测到另一个“碰撞”,并再次反转速度


结果是你的球在拦网边缘抖动,不断反转速度。

反弹
功能中,
if
else if
没有覆盖所有值。,我建议您将
>
替换为
=
当您说它不会在墙上反弹时,您的意思是它只是离开屏幕或停止?@SeanF它只是停止。木块是实心的,但我把球和木块对齐了。查看bounce(GObject)方法要调试此问题,我建议在更新步骤中打印出球的速度。我使用<和>,因为如果值为零,则反转速度是一件无用的事情。