Hi everyone. I’ve recently been adding a simple physics engine to my 2D game engine that I will hopefully be using in my future projects. Right now I have two colliders, a BoundingBox (essentially an AABB) and a BoundingCircle class. Both of these extend my abstract BoundingVolume class. The BoundingBox is defined by an X, Y, Width and Height. The BoundingCircle is defined by an X, Y (the center of the circle) and a radius.
I check collisions between two BoundingBox’s like this:
public boolean intersects(BoundingBox other) {
if(other.getY() > (getY()+getHeight()) || //check top edge
(other.getY()+other.getHeight()) < getY() || //check bottom edge
other.getX() > (getX()+getWidth()) || //check right edge
(other.getX()+other.getWidth()) < getX()){ //check left edge
//NO COLLISION
return false;
}
return true;
}
And between two BoundingCircle’s like this:
public boolean intersects(BoundingCircle other) {
//get the distance between the two circle's centers
Vector2f distance = new Vector2f(other.getCenter().getX() - center.getX(), other.getCenter().getY() - center.getY());
if(distance.getLength() <= radius + other.getRadius()){
//COLLISION!
return true;
}
return false;
}
But how would I check for a collision between the BoundingBox and the BoundingCircle? I’ve looked into the Separating Axis Theorem and I don’t think that it can work for circles as they have an infinite amount of sides. I also don’t want to use an open source physics library as I want the learning experience. What is a simple and fast way of checking for a collision between a rectangle and a circle?
P.S. Tell me if you see anything wrong with my way of checking between rectangle-rectangle and circle-circle collisions as well.
Thank you!