recCir method
Checks if two entities with circle and rectangle shape overlap or touch by checking the boundaries of both entities
Implementation
static bool recCir(Entity rectangle, Entity circle) {
if (!CollisionChecker.rectangles(rectangle, circle)) {
return false;
}
double r = (circle.width / 2);
double x = circle.x + r;
double y = circle.y + r;
// This will return false when the circle is outside the corner of the rectangle
// Upper left
if (x < rectangle.x && y < rectangle.y) {
if ((r < sqrt(pow(x - rectangle.x, 2) + pow(y - rectangle.y, 2)))) {
return false;
}
}
// Upper right
if (x > rectangle.x + rectangle.width && y < rectangle.y) {
if ((r < sqrt(pow(x - (rectangle.x + rectangle.width), 2) + pow(y - rectangle.y, 2)))) {
return false;
}
}
// Lower left
if (x < rectangle.x && y > rectangle.y + rectangle.height) {
if ((r < sqrt(pow(x - rectangle.x, 2) + pow(y - (rectangle.y + rectangle.height), 2)))) {
return false;
}
}
// Lower right
if (x > rectangle.x + rectangle.width && y > rectangle.y + rectangle.height) {
// Radius is greater than the distance to the corner
if ((r < sqrt(pow(x - (rectangle.x + rectangle.width), 2) + pow(y - (rectangle.y + rectangle.height), 2)))) {
return false;
}
}
return true;
}