I have been creating a game using C++ and the graphics library SFML. I want a boolean method to check if there is a collision between a sf::CircleShape and a sf::RectangleShape given those two objects as parameters. The problem is that the objects are not axis aligned. How would I create the method?
My attempt at the method (but it only works for axis aligned shapes):
bool checkCollision(CircleShape circle, RectangleShape rectangle)
{
double circleDistanceX = abs(circle.getPosition().x - rectangle.getPosition().x);
double circleDistanceY = abs(circle.getPosition().y - rectangle.getPosition().y);
if (circleDistanceX > (rectangle.getSize().x/2 + circle.getRadius()))
{
return false;
}
if (circleDistanceY > (rectangle.getSize().y/2 + circle.getRadius()))
{
return false;
}
if (circleDistanceX <= (rectangle.getSize().x/2))
{
return true;
}
if (circleDistanceY <= (rectangle.getSize().y/2))
{
return true;
}
double cornerDistance_sq = pow((circleDistanceX - rectangle.getSize().x/2), 2) + pow((circleDistanceY - rectangle.getSize().y/2),2);
return (cornerDistance_sq <= pow((circle.getRadius()),2));
}