点和矩形碰撞检测?
Point and Rectangle collision detection?
所以我创建了一种检测 BallView 球和 Rectangle 矩形之间碰撞的方法。我把它分成了 4 个部分;它检测圆的最顶部、圆的最左边、圆的最底部和圆的最右边与矩形之间的碰撞。其中两个工作是检测圆上的左点与矩形之间的碰撞,以及检测圆上的右点与矩形之间的碰撞。但是,如果最上面的点或最下面的点接触矩形没有检测到碰撞,这是行不通的,正如我在记录 if 语句以查看是否输入时所看到的那样。这是我的代码(方法 .getR() 获取圆半径):
public boolean intersects(BallView ball, Rectangle rect){
boolean intersects = false;
//Left point
if (ball.getX() - ball.getR() >= rect.getTheLeft() &&
ball.getX() - ball.getR()<= rect.getTheRight() &&
ball.getY() <= rect.getTheBottom() &&
ball.getY() >= rect.getTheTop())
{
intersects = true;
}
//Right point
if (ball.getX() + ball.getR() >= rect.getTheLeft() &&
ball.getX() + ball.getR() <= rect.getTheRight() &&
ball.getY() <= rect.getTheBottom() &&
ball.getY() >= rect.getTheTop())
{
intersects = true;
}
//Bottom point (wont work?)
if (ball.getX() >= rect.getTheLeft() &&
ball.getX() <= rect.getTheRight() &&
ball.getY() + ball.getR() <= rect.getTheBottom() &&
ball.getY() + ball.getR()>= rect.getTheTop())
{
intersects = true;
}
//Top point (wont work?)
if (ball.getX() >= rect.getTheLeft() &&
ball.getX() <= rect.getTheRight() &&
ball.getY() - ball.getR()<= rect.getTheBottom() &&
ball.getY() - ball.getR()>= rect.getTheTop())
{
intersects = true;
}
return intersects;
}
我已经考虑到 Y 会随着你往下走而增加,而 X 会随着你向右走而增加。为什么此方法不能检测圆边缘上顶点和底点的交点?另外,屏幕是横向的。
看起来这只是一个逻辑错误,我假设 .getX 和 .getY 是球心的 x 和 y 坐标,以及 .getR 是半径,所以你想要你的代码看起来像这样
if (ball.getX() + ball.getR() >= rect.getTheLeft() &&
ball.getX() - ball.getR() <= rect.getTheRight() &&
ball.getY() - ball.getR() <= rect.getTheBottom() &&
ball.getY() + ball.getR() >= rect.getTheTop())
{
intersects = true;
}
我不确定你的应用程序的具体细节,但我不确定你是否需要更多的东西来判断球是否与矩形相交
所以我创建了一种检测 BallView 球和 Rectangle 矩形之间碰撞的方法。我把它分成了 4 个部分;它检测圆的最顶部、圆的最左边、圆的最底部和圆的最右边与矩形之间的碰撞。其中两个工作是检测圆上的左点与矩形之间的碰撞,以及检测圆上的右点与矩形之间的碰撞。但是,如果最上面的点或最下面的点接触矩形没有检测到碰撞,这是行不通的,正如我在记录 if 语句以查看是否输入时所看到的那样。这是我的代码(方法 .getR() 获取圆半径):
public boolean intersects(BallView ball, Rectangle rect){
boolean intersects = false;
//Left point
if (ball.getX() - ball.getR() >= rect.getTheLeft() &&
ball.getX() - ball.getR()<= rect.getTheRight() &&
ball.getY() <= rect.getTheBottom() &&
ball.getY() >= rect.getTheTop())
{
intersects = true;
}
//Right point
if (ball.getX() + ball.getR() >= rect.getTheLeft() &&
ball.getX() + ball.getR() <= rect.getTheRight() &&
ball.getY() <= rect.getTheBottom() &&
ball.getY() >= rect.getTheTop())
{
intersects = true;
}
//Bottom point (wont work?)
if (ball.getX() >= rect.getTheLeft() &&
ball.getX() <= rect.getTheRight() &&
ball.getY() + ball.getR() <= rect.getTheBottom() &&
ball.getY() + ball.getR()>= rect.getTheTop())
{
intersects = true;
}
//Top point (wont work?)
if (ball.getX() >= rect.getTheLeft() &&
ball.getX() <= rect.getTheRight() &&
ball.getY() - ball.getR()<= rect.getTheBottom() &&
ball.getY() - ball.getR()>= rect.getTheTop())
{
intersects = true;
}
return intersects;
}
我已经考虑到 Y 会随着你往下走而增加,而 X 会随着你向右走而增加。为什么此方法不能检测圆边缘上顶点和底点的交点?另外,屏幕是横向的。
看起来这只是一个逻辑错误,我假设 .getX 和 .getY 是球心的 x 和 y 坐标,以及 .getR 是半径,所以你想要你的代码看起来像这样
if (ball.getX() + ball.getR() >= rect.getTheLeft() &&
ball.getX() - ball.getR() <= rect.getTheRight() &&
ball.getY() - ball.getR() <= rect.getTheBottom() &&
ball.getY() + ball.getR() >= rect.getTheTop())
{
intersects = true;
}
我不确定你的应用程序的具体细节,但我不确定你是否需要更多的东西来判断球是否与矩形相交