我用一個圓形和一個矩形進行簡單的2D碰撞檢測時遇到了問題。所有檢查器都會看到圓的中心是否與矩形上的任何點重疊。這個簡單的圓形/矩形碰撞檢測有什麼問題?
繼承人的檢查:
boolean overlaps(Circle circle) {
for (double i = topLeft.x; i < (topLeft.x + width); i++)
{
for (double j = topLeft.y; j < (topLeft.y + height); j++)
{
if (circle.center.x == i && circle.center.y == j)
{
return true;
}
}
}
return false;
}`
很簡單,給點意見。 if語句出現錯誤,我的circle.center.x
和/或circle.center.y
。
這裏是我的Circle類代碼:
public class Circle {
Point center;
double radius;
/**
* Constructor.
*
* @param center the center Point of the circle
* @param radius the radius of the circle
*/
public Circle(Point center, double radius) {
center = this.center;
radius = this.radius;
}
/**
* Get the current center point of the circle.
*
* @return the center point of the circle
*/
public Point getCenter() {
return center;
}
/**
* Set the center point of the circle.
*
* @param center the (new) center point of the circle
*/
public void setCenter(Point center) {
this.center = center;
}
/**
* Get the current radius.
*
* @return the current radius
*/
public double getRadius() {
return radius;
}
/**
* Set the radius.
*
* @param radius the (new) radius of the circle
*/
public void setRadius(double radius) {
this.radius = radius;
}
}`
我在哪裏去了?它肯定不會成爲我的Point類的一個問題,因爲這意味着很多其他事情現在會出錯。提前致謝。
爲什麼二維循環?爲什麼不簡單不平等(即'<' or '>')檢查? – 2014-10-07 22:30:58
我確實改變了它的建議,但問題仍然存在於圈子類中。 – GravityCheck 2014-10-07 22:39:23