Rectangle Overlaps Rectangle

Hi. I need to detect if two rectangles ever overlap or come on top of eachother/collide in a 2d space.

bool RectContainsRect(Rect rA, Rect rB)
    {
        bool Bool = false;
        if (((rA.xMin >= rB.xMin && rA.xMin <= rB.xMax) &&
                        (rA.yMin >= rB.yMin && rA.yMin <= rB.yMax)) ||
                        ((rA.xMax >= rB.xMin && rA.xMax <= rB.xMax) &&
                        (rA.yMax >= rB.yMin && rA.yMax <= rB.yMax)) ||
                        ((rA.x >= rB.xMin && rA.x <= rB.xMax) &&
                        (rA.y >= rB.yMin && rA.y <= rB.yMax)) ||
                        ((rB.xMin >= rA.xMin && rB.xMax <= rA.xMax) &&
                        (rB.yMin >= rA.yMin && rB.yMax <= rA.yMax)) ||
                        ((rB.xMin >= rA.xMin && rB.xMin <= rA.xMax) &&
                        (rB.yMin >= rA.yMin && rB.yMin <= rA.yMax)) ||
                        ((rB.xMax >= rA.xMin && rB.xMax <= rA.xMax) &&
                        (rB.yMax >= rA.yMin && rB.yMax <= rA.yMax)) ||
                        ((rB.x >= rA.xMin && rB.x <= rA.xMax) &&
                        (rB.y >= rA.yMin && rB.y <= rA.yMax)) ||
                        ((rA.xMin >= rB.xMin && rA.xMax <= rB.xMax) &&
                        (rA.yMin >= rB.yMin && rA.yMax <= rB.yMax))) {
            Bool=true;
                }
        return Bool;
            }

Is this the right way?

bool RectOverlapsRect (Rect rA, Rect rB) {
    return (rA.x < rB.x+rB.width && rA.x+rA.width > rB.x && rA.y < rB.y+rB.height && rA.y+rA.height > rB.y);
}

–Eric

1 Like

Isn’t rA.x center X? So then rA.x+rA.width would give offset.

Also what if the two rectangles overlapping form a cross? Neither of the edges are inside the other rectangle so what is the way to find out?

What I need to know if any one point inside a rectangle is inside the other.
But thank you for the fast response.

I’d advise to test those values with those examples you give, because as far as I can see, it covers every case.

For example, it doesn’t matter at all if rA.x is the center, top right, bottom left, nor does it matter if it offsets.

What you want to know is IF they intersect at all, not to determine the coordinates where they intersect.

I know it’s a bit confusing at first, because it can feel a bit counter intuitive, but his formula is basically the very application of the mathematical formulas for that very question, in the cleanest form, both for the reading and for the machine operations.

Again, the best part is that you don’t have to blindly believe, you should be able to try it pretty easily by setting up cases and calling the function on those.

Nope (rA.center is the center, and rA.x is the same as rA.xMin; see the docs), but yeah it doesn’t actually matter. Although it could have been simplified by doing stuff like rA.xMax instead of rA.x+rA.width (same thing).

–Eric

To the OP - If you never plan to rotate your Rectangles (or if you have some algorithm that will transform the rects to their normal orientation), this algorithm will work.

If you do plan on rotating the rectangles, this algorithm may fail.

You’re probably already aware of this, just giving you a word of caution in case you aren’t. Consider using Colliders or Line equations (not necessarily linecasts/raycasts, but Point/Direction lines) to detect these kind of collisions.