Give a rect s1 = {x, y, w, h}
and another rects s2 = {x, y, w, h}
and a vector v = {x, y}
. And assuming that s1
has moved according to v
, I want to check if it is overlapping.
I have this algorithm:
isOverlapping = not (s1.x + s1.w + v.x < s2.x
or s1.x + v.x > s2.x + s2.w
or s1.y + s1.h + v.y < s2.y
or s1.h + v.y > s2.y + s2.h)
But it doesn't seem to work correctly, but I can't even say what is wrong with it, because I don't understand it at all. And because of its nature I can't even seem to break it apart into smaller pieces.
isOverlapping = not (right_edge_of_s1_plus_move < left_edge_of_s2
or left_edge_of_s1_plus_move > right_edge_of_s2
or top_edge_of_s1_plus_move < bottom_edge_of_s2
or bottom_edge_of_s1_plus_move > top_edge_of_s2)
isOverlapping = not (s1_overlaps_s2_on_left_edge
or s1_overlaps_s2_on_right_edge
or s1_overlaps_s2_on_bottom_edge
or s1_overlaps_s2_on_top_edge)
That implies that they are not not overlapping as soon as s1 overlaps s2 on one edge... What?
The weird thing is, that in my program, it only does not work, when s1 tries to move under s2. Everything else works.
So my question is: What is the correct algorithm for testing if two moving boxes are overlapping each other (AABB) and what kind of algorithm do I have here? I know I got it from somewhere, but I can't find my source anymore. I just added the extra movement vector.