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
編集
これが解決策です。バウンス()メソッドをこれに変更しました。
/**
* 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;
}
}