Advertisement

Need Help Declaring Variables

Started by June 05, 2015 10:55 PM
9 comments, last by Oberon_Command 9 years, 8 months ago

Hi !

(Note : I am using C++ and SDL 2.0 Library)

I am facing some problems in declaring the values of some integer variables.

Let's say that I have three variables, X , Y and Z.

Y equals 10 and Z equals 25.

I want the value of X to be EVERYTHING more than Y and less than Z. (11, 12, 13, ..., 22, 23, 24)

How can I approach this ?

Thanks in advance

EDIT : I also have A , B and C.

A is equal to EVERYTHING more than B and less than C.

I want to check if any integer in X is equal to any integer in A.

Simply I want to create two ranges of integers and check if any integer is present in both.

Can I do this ? If I can , how ?

Thanks again

If you want to just store all of those values for later use, you could use an array or a std::vector. Or you could just use a loop in places where you need X.

What are you trying to do, exactly?

Advertisement

If you want to just store all of those values for later use, you could use an array or a std::vector. Or you could just use a loop in places where you need X.

What are you trying to do, exactly?

Thanks for the fast response

What I was trying to do is a simple collision system by detecting if the position any right point from texture B is more than that of any left point from texture A

Unless I'm misunderstanding what you're trying to accomplish, why not create SDL_Rects that represent your quads and then use SDL_HasIntersection to see if they intersect?

EDIT : I also have A , B and C.

A is equal to EVERYTHING more than B and less than C.

I want to check if any integer in X is equal to any integer in A.

Simply I want to create two ranges of integers and check if any integer is present in both.


There's an easier way to think about this. What you actually have is two ranges, and you want to see if they intersect. If two ranges on a single axis intersect, then an end-point of one of the ranges is within the other range.

[source]
bool range_intersects(int startFirst, int endFirst, int startSecond, int endSecond)
{
return (startFirst >= startSecond && startFirst <= endSecond) // test if left side of the first range is in the second range
|| (endFirst >= startSecond && endFirst <= endSecond); // test if the right side of the first range is in the second range
}
[/source]

Unless I'm misunderstanding what you're trying to accomplish, why not create SDL_Rects that represent your quads and then use SDL_HasIntersection to see if they intersect?

EDIT : I also have A , B and C.

A is equal to EVERYTHING more than B and less than C.

I want to check if any integer in X is equal to any integer in A.

Simply I want to create two ranges of integers and check if any integer is present in both.


There's an easier way to think about this. What you actually have is two ranges, and you want to see if they intersect. If two ranges on a single axis intersect, then an end-point of one of the ranges is within the other range.

[source]
bool range_intersects(int startFirst, int endFirst, int startSecond, int endSecond)
{
return (startFirst >= startSecond && startFirst <= endSecond) // test if left side of the first range is in the second range
|| (endFirst >= startSecond && endFirst <= endSecond); // test if the right side of the first range is in the second range
}
[/source]

Thanks for the response

SDL_HasIntersection needs Const Rects which I don't use

And for the second code , how would you declare the variables ??

For Example

Rect1 Position (0, 0) - Rect2 Dimensions (200, 300)

Rect2 Position (600, 700) - Rect2 Dimensions (100, 100)

How can I detect collisions ?

Thanks again

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.
Advertisement

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.

Thanks very much

Actually , I got HasIntersection working right after I wrote my last reply (I looked at my RenderCopy code which also requires const SDL_Rect)

But After I detect that there is intersection , what will I do ??

HasIntersection just tells if there is an intersection or not and there is now way that I can check the last position of the first rect before intersection so I can prevent it from intersection.

Thanks really very very very much

OK I wrote this code but I don't know why it doesn't work. if (PlayerRect.x = EnemyRect.x) { if (PlayerRect.y < EnemyRect.y + EnemyRect.h) { PlayerRect.y = EnemyRect.y + EnemyRect.h; } else if (PlayerRect.y + PlayerRect.h > EnemyRect.y) { PlayerRect.y = EnemyRect.y - PlayerRect.h; } } if (PlayerRect.y = EnemyRect.y) { if (PlayerRect.x < EnemyRect.x + EnemyRect.w) { PlayerRect.x = EnemyRect.x + EnemyRect.w; } else if (PlayerRect.x + PlayerRect.w > EnemyRect.x) { PlayerRect.x = EnemyRect.x - PlayerRect.w; } } Whenever I start debugging , the two rects stick two each other and I if I try to move PlayerRect it returns and sticks with EnemyRect.

But After I detect that there is intersection , what will I do ??


Do whatever you want to do when a collision is detected. That's the purpose of a collision system - to notify the rest of the game that a collision has occurred.

HasIntersection just tells if there is an intersection or not and there is now way that I can check the last position of the first rect before intersection so I can prevent it from intersection.


Why don't you just preserve the last position of the rectangles, too?

But After I detect that there is intersection , what will I do ??


Do whatever you want to do when a collision is detected. That's the purpose of a collision system - to notify the rest of the game that a collision has occurred.

HasIntersection just tells if there is an intersection or not and there is now way that I can check the last position of the first rect before intersection so I can prevent it from intersection.


Why don't you just preserve the last position of the rectangles, too?

How ?

This topic is closed to new replies.

Advertisement