Wednesday, January 28, 2015

Circle to Rectangle Collision 2

I finished the function yesterday for circle to rectangle collision. From my problem solving, I realized the best way was to first check if the center of the circle lies within the green areas:




with c being the circle's properties, and r being the rectangle's properties.

if ((cX >= rX && cX <= rX + rWidth) || (cY >= rY && cY <= rY + rHeight))
{
    check for rectangle on rectangle collision;
}

So right there, the circle becomes a rectangle and you just need a normal rectangle collision check.


However, if that conditional statement evaluates to false, i.e. the center of the circle lies outside of the green areas, then find out which non green quadrant the center of the circle is in and store the rectangle's corner coordinate. Like I was saying in the previous post, outside of the green areas, the rectangle's corner point will be the first thing to collide with the circle.

with p being the rectangle's corner coordinates and radius the circle's radius.

if (FP.distance(cX, cY, pX, pY) <= radius) return true;




No comments:

Post a Comment