Rectanglei x = Rectanglei(Vector2i(0, 0), Vector2i(640, 320));
Rectanglei y = Rectanglei(Vector2i(102, -20), Vector2i(256, 256));
auto result = is_colliding_yes(x, y);
printf("%d, %d, %d, %d\n", result[0], result[1], result[2], result[3]);
printf("%d + %d >= %d\n", x.pos.y, x.size.y, y.pos.y);
printf("%d\n", x.pos.y + x.size.y >= y.pos.y);
bool is_colliding(const Rectanglei x, const Rectanglei y) {
return (x.pos.x + x.size.x >= y.pos.x &&
x.pos.x <= y.pos.x + y.size.x &&
x.pos.y + x.size.y >= y.pos.y &&
x.pos.y <= y.pos.y + y.size.y);
}
// For debugging
std::array<bool, 4> is_colliding_yes(const Rectanglei x, const Rectanglei y) {
return {
x.pos.x + x.size.x >= y.pos.x,
x.pos.x <= y.pos.x + y.size.x,
x.pos.y + x.size.y >= y.pos.y,
x.pos.y <= y.pos.y + y.size.y
};
}
struct Rectanglei {
Vector2i pos, size;
};
Output:
1, 1, 0, 1
0 + 320 >= -20
0
When the left upper corner of rectangle y is to the left or top of rectangle x it always returns false
