【发布时间】:2015-02-28 01:46:27
【问题描述】:
所以我创建了一个方法来检测 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 会随着您向右而增加。为什么这种方法不能检测圆边缘上顶点和底部的交点?此外,屏幕是横向的。
【问题讨论】:
标签: java android collision-detection