如何通过矩形之间的相交来检查碰撞是否有效?

编辑:这里是完整的代码: https : //dl.dropboxusercontent.com/u/65678182/assignment1.rar任何可能的帮助非常感谢!


我正在尝试制作突破游戏的副本,但我在检查两个物体(球和桨)是否相交时遇到问题。

我现在有这种碰撞检测方法:

public static void handleCollisions() { System.out.println(ball.getBounds()); // just to check that they System.out.println(paddle.getBounds()); //are there and moving correct if (ball.getBounds().intersects(paddle.getBounds())) { System.out.println("collision"); } } 

我很确定getBounds的工作原理应该是因为我从println获得这些输出:java.awt.Rectangle [x = 393,y = 788,width = 14,height = 14] java.awt.Rectangle [x = 350,Y = 350,宽度= 100,高度= 10]

 getBounds code: public static Rectangle getBounds() { return new Rectangle(x, y, radius*2, radius*2); } 

我可以看到它们在一个点上移动它们是重叠的,但该方法从未检测到它。

我对这个很新,所以希望它在某个地方只是一些愚蠢的错误,任何帮助都会受到赞赏。 如果有必要,我可以发布更多代码,但不愿意。

 java.awt.Rectangle[x=393,y=788,width=14,height=14] java.awt.Rectangle[x=350,y=350,width=100,height=10] 

你可以看到第二个矩形的y /高度为350/10,但第一个的y = 788

显然,他们没有一个在另一个之上的交叉点

更新还有一件事

 public static Rectangle getBounds() { return new Rectangle(x, y, radius*2, radius*2); } 

如果x和y是球的中心,那么代码应该是

 public static Rectangle getBounds() { return new Rectangle(x-radius, y-radius, radius*2, radius*2); }