0
\$\begingroup\$

Hello GameDev stack exchange!

I'm adapting a rectangle to circle collision detection algorithm from the second (not the accepted) answer from here: https://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection. This is C++ code. Here is my adaptation:

bool Collision::checkCircleToRectCollision(sf::CircleShape circle, float rectX, float rectY, float width, float height)
{   
    sf::Vector2f circleDistance;

    circleDistance.x = std::abs((circle.getPosition().x - rectX) + circle.getRadius());
    circleDistance.y = std::abs((circle.getPosition().y - rectY) + circle.getRadius());

    if (circleDistance.x > (width / 2 + circle.getRadius())) {
        return false;    
    }  

    if (circleDistance.y > (height / 2 + circle.getRadius())) {
        return false;
    }

    if (circleDistance.x <= (width / 2)) {
        return true;
    }

    if (circleDistance.y <= (height / 2)) {
        return true;
    }

    float cornerDistance = ((circleDistance.x - width / 2) * (circleDistance.x - width / 2)) +
                          ((circleDistance.y - height / 2) * (circleDistance.y - height / 2));

    return cornerDistance <= (circle.getRadius() * circle.getRadius());
}

The problem is the collision detects only the lower right section of the circle. Without the + circle.getRadius() inside of circleDistance.x = std::abs((circle.getPosition().x - rectX) the algorithm detects even less area. Here is a a picture to visualize. I want the algorithm to detect the entire blue area. It currently only detects the red area. This is a an approximate, but very accurate from what I can tell. The white box is my players hitbox.

result

I've spent a few days googling and this is the closest I've gotten, but now I'm stuck. How can I make the algorithm detect the entire blue circle? Thanks in advance! :)

\$\endgroup\$
8
  • \$\begingroup\$ Wouldn't you need the distance formula? \$\endgroup\$
    – clabe45
    Commented Aug 22, 2017 at 17:59
  • \$\begingroup\$ Can you clarify what you mean? What/which distance formula? \$\endgroup\$ Commented Aug 22, 2017 at 18:55
  • \$\begingroup\$ I see a lot of distance variables, but no distance formula (like sqrt((x2-x1)^2 + (y2-y1)^2) ) \$\endgroup\$
    – clabe45
    Commented Aug 22, 2017 at 18:56
  • \$\begingroup\$ So it seems that the author of my linked stackoverflow answer left that out. Let me try your square root and get back to you. \$\endgroup\$ Commented Aug 22, 2017 at 18:59
  • \$\begingroup\$ He used the distance formula (in a way) at the very end of the function cornerDistance_sq = (circleDistance.x - rect.width/2)^2 + (circleDistance.y - rect.height/2)^2; return (cornerDistance_sq <= (circle.r^2)); \$\endgroup\$
    – clabe45
    Commented Aug 22, 2017 at 19:01

1 Answer 1

1
\$\begingroup\$

So it seems that I was passing my function described in the original post the wrong values. Passing the correct values works well enough that I'm marking this as solved and answering my own question. I changed the following in main.cpp:

if (collision.checkCircleToRectCollision(circle, player.boundingBox.getPosition().x,
                                                 player.boundingBox.getPosition().y, 
                                                 5, 5)) {
     //Collision
}

to use the correct player values:

if (collision.checkCircleToRectCollision(circle, player.boundingBox.getPosition().x,
                                                 player.boundingBox.getPosition().y, 
                                                 player.size.x, player.size.y)) {
     //Collision
}

And all is well. Thanks anyways! :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .