У меня проблема с простым обнаружением столкновения 2d с кругом и прямоугольником. Вся проверка проверяет, совпадает ли центр круга с любой точкой на прямоугольнике.Что не так с этим простым обнаружением столкновения круга/прямоугольника?
Heres шашка:
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, поскольку это означало бы, что многие другие вещи поступили бы неправильно. Заранее спасибо.
Почему 2D цикл? Почему не простое неравенство (т. Е. '<' or '>') проверяет? –
Я действительно изменил его на то, что предложил clcto, но проблема все еще лежит в классе круга. – GravityCheck