在使用JavaSE写一个游戏(坦克大战)的时候,需要进行碰撞检测,比如子弹与坦克,坦克与坦克,坦克与墙等。
我的解决办法是利用一个Rectangle对象,让他和坦克或墙相重合,用intersects()方法判断他们是否相交。
但是实际的情况是,虽然这个Rectangle对象和墙的x、y、宽和高都相同(子弹 同理),但在运行时,图片和Rectangle对象却不在同一位置。
这个Rectangle对象好像向下和向右移动了
墙类
public abstract class BaseWall extends GameObject{
private int x,y;
//宽高为资源图片的宽高
public static int width=ResourceMgr.wall.getWidth(),height=ResourceMgr.wall.getHeight();
public Rectangle rect;
public BaseWall(int x, int y) {
this.x = x;
this.y = y;
//构造方法对Rectangle初始化
this.rect=new Rectangle(x,y,width,height);
System.out.println(this.x+":"+this.y+":"+this.width+":"+this.height);
System.out.println(rect.x+":"+rect.y+":"+rect.width+":"+rect.height);
}
子弹的构造方法同理
碰撞的逻辑:
if(wall.rect.intersects(bullet.getRect())) {
bullet.die();
return true;
}
Rectangle和Frame存在这种BUG吗?