SDL_HasIntersection needs Const Rects which I don't use
You're misunderstanding how const works. If you see a function that takes a const pointer (or a const reference) as an argument, that means that the function is promising that it will not modify the thing the argument is pointing to. It doesn't mean you have to pass in a const SDL_Rect*. You can pass something mutable into something expecting a const, but you can't go the other way and modify something that you get that's const. If that weren't the case, you wouldn't be able to pass a non-const char * as the first argument to
printf.
I suggest reading up on "const correctness" if you're interested in more information about how that works. I also suggest that it's a good idea to make everything you can const, as it's been shown that accidentally changing variables at the wrong time/in the wrong place is a common source of bugs.
And for the second code , how would you declare the variables ??
Rect1 Position (0, 0) - Rect2 Dimensions (200, 300)
Rect2 Position (600, 700) - Rect2 Dimensions (100, 100)
If you're really averse to SDL_Rect for some reason, you could use the function I showed you like this:
[source]
// rect 1
int rect1_x = 0;
int rect1_y = 0;
int rect1_width = 200;
int rect1_height = 300;
// rect 2
int rect2_x = 600;
int rect2_y = 700;
int rect2_width = 100;
int rect2_height = 100;
// using the function I gave in my last post
if (range_intersects(rect1_x, rect1_x + rect1_width, rect2_x, rect2_x + rect2_width) && range_intersects(rect1_y, rect1_y + rect1_height, rect2_y, rect2_y + rect2_height))
{
// ... the rectangles are intersecting, collision detected
}
// or you could even just do this, since you already know every value at compile-time
if (range_intersects(0, 200, 600, 700) && range_intersects(0, 300, 700, 800))
{
// ... the rectangles are intersecting, collision detected
}
[/source]
But I don't recommend this when you have a perfectly valid solution provided by SDL already, since it means that you wouldn't have to write the function I just did in the first place and the code is much shorter.
[source]
// note that these are not const, but they could be (and probably should be) if we wanted them to be.
SDL_Rect rect1 = { 0, 0, 200, 300};
SDL_Rect rect2 = {600, 700, 100, 100};
if (SDL_HasIntersection(&rect1, &rect2))
{
// .. the rectangles are intersecting, collision detected
}
[/source]
Where are you getting those numbers, by the way?
Also, I think this thread is "For Beginners" material.