Intersection of two rectangles

Hi everyone!
I’ve tried to create a rectangle class and check if two rectangles intersect. Their x and y values represent the centers. My intersection condition looks like this:

r1.x+r1.w/2 < r2.x-r2.w/2 || r2.x+r2.w/2 < r1.x-r1.w/2 ||
r1.y-r1.h/2 < r2.y+r2.h/2 || r2.y-r2.h/2 < r1.y+r1.h/2

but for some reason, it always returns true. I would highly appreciate it if you can help me find the mistake.
Thanks in advance

Processing Java Mode Rect-Rect: Collision Detection
Flavor p5js Rect-Rect: Collision Detection

4 Likes

In case you want to dive deeper into intersections this article by gorillasun is a fantastic resource:

6 Likes