我创建了一个游戏,pacman专门作为我的课程作业的一部分,当碰到使用矩形的碰撞检测时我遇到了问题。Java:帮助确定两个矩形之间的碰撞
我遇到的问题是即使在屏幕上看到字符不会碰撞检查交叉口实际上总是返回true。下面的输出解释了我的意思是:
吃豆子细节:X 17.0Ÿ16.0 漆黑的细节:X 22.0Ÿ调用相交()后13.0 碰撞:真 吃豆子细节:X 18.0Ÿ16.0 漆黑的详细信息: X 23.0Ÿ调用相交()后13.0 碰撞:真
我已经矩形设置如下:
public Rectangle pacmanBounds(){
return new Rectangle(pacRow, pacColumn, 22, 22);
}
public Rectangle ghostBounds(){
return new Rectangle(ghostRow, ghostColumn, 22, 22);
}
高度和宽度已经过硬编码用于测试目的,但这些是实际的图像大小。
我检查碰撞如下每次JPanel的重新绘制:在时间
public boolean checkCollision(){
Rectangle pacmanBounds = pacman.pacmanBounds();
//currently commented out for testing
/*if(pacmanBounds.intersects(inky.ghostBounds()) || pacmanBounds.intersects(blinky.ghostBounds())
|| pacmanBounds.intersects(pinky.ghostBounds()) || pacmanBounds.intersects(clyde.ghostBounds())){
System.out.println("Oh no!");
return true;
}*/
System.out.println("Pacman details: x " + pacmanBounds.getX() + " y " + pacmanBounds.getY() + " ");
System.out.println("Inky details: x " + inky.ghostBounds().getX() + " y " + inky.ghostBounds().getY());
System.out.println("Collision after calling intersects(): " + pacmanBounds.intersects(inky.ghostBounds()));
return false;
}
在这一点上我已经差不多用完了关于如何解决这个问题,所以任何意见,你们的想法可以给予不胜感激!
很明显看到这两个矩形相交:{(17,16),(39,38)}和{(22,13),(44,35)}。我想问题是为什么图像看起来不像它相交?它有一个很大的透明边框吗? – rlibby 2011-04-09 01:40:21
谢谢你的评论rlibby,你说的对十字路口我想我完全忘了矩形是如何工作的!我已经缩小了矩形的大小,它正在工作:) – tfstorm 2011-04-09 02:06:02