#Function doesn't detect that rectangles are colliding even though they should

25 messages · Page 1 of 1 (latest)

molten tulip
#
    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

cerulean sparrowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

molten tulip
#

turns out the vector2i uses unsigned integers

#

!solved

cerulean sparrowBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

rustic crater
#

Nice, you figured it out on your own. I would also recommend picking better names for variables. Like x.pos.y is so confusing to read

violet bolt
# timber jacinth printf?

unironically can be faster/easier to write and read than a std::cout chain, especially if you were to also add format specifiers like, e.g., %04d.
At least that's my opinion

timber jacinth
#

its the error prone-ness

violet bolt
molten tulip
#

what is this conversation 💀

molten tulip
#

If I used std::cout with BITWISE SHIFTS it would probably print as an unsigned value and I would know immediately know what the problem was

#

I just use printf because I've used C longer than C++

molten tulip
timber jacinth
#

std::cout << "foo" is NOT a bitwise shift

molten tulip
#

but overloaded

rustic crater
timber jacinth
timber jacinth
molten tulip
timber jacinth