1. ## Check collision (REAL THREAD)

Hi peeps!

Got a poblem here. Can't make my check collision function to work. Not much to say is there? Oh right, when collision happens, i want to move the square somewhere on the surface with a specific height. Got it? Good here's the code:

Main:
Code:
```    //The square's coordinates
int sX = 400;
int sY = 300;

//Check collision and move the square
check_coll(sX, sY, x, y);```
Check collision:
Code:
```//Check if there is a collision
int check_coll( int sX, int sY, int pX, int pY )
{
//Square's position
int sXl = sX;
int sXr = sX + 29;
int sYl = sY;
int sYr = sY + 29;

//Piece's position
int pXl = pX;
int pXr = pX + 75;
int pYl = pY;
int pYr = pY + 148;

//Check if you hit the square
int n  = sXl;
if (sXl == pXr || sXr == pXl)
{
//New position for the square
n = GetRand(0, 640);

SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );
apply_surface( 0, 0, background, screen );
apply_surface( pXl, pYl, piece, screen );
}

//Apply the square
apply_surface( n, sYl, square, screen );
}```
Random number:
Code:
```//Random number
int GetRand(int min, int max)
{
static int Init = 0;
int rc;

if (Init == 0)
{
srand(time(NULL));
Init = 1;
}

rc = (rand() % (max - min + 1) + min);

return (rc);
}```
I think this is all code that is necessary. If you need more code, tell me!

Problems:
1. When if (sXl == pXr || sXr == pXl) gets true, the square switches position instead of staying. So the random function goes weird. It should only get 1 number but it don't.
2. When if (sXl == pXr || sXr == pXl) has stopped being true, the square goes back to the start position of the square.

How do i solve theese problems?

REGARDS!

2. You will have to make sX and sY reference parameters to do all that inside the check_coll() function.
Kurt

3. i saw that check_col function returns an int value, but it didnt

4. Should it not be && instead of || ?

Anyway, here is almost the same thing I did a while ago. Its not the best way to do it, but it works
Code:
```void ball::update()
{
....

// Collsion against player1
if ( (posX > player1.getPosX()) && ((posY < (player1.getPosY() + player1.getSizeY())) && (posY > (player1.getPosY() - player1.getSizeY()))) )
{
velX = (velX *-1) * 1.1;
velY = ((velY *1) * 1.1);
}

// Collsion against player2
if ( (posX < player2.getPosX()) && ((posY < (player2.getPosY() + player2.getSizeY())) && (posY > (player2.getPosY() - player2.getSizeY()))) )
{
velX = (velX *-1) * 1.1;
velY = ((velY *1) * 1.1);
}

// Collision against roof
if (posY > 1){
velY = velY * -1;}
// Collision against floor
if (posY < -1){
velY = velY * -1;}

....
}```

5. ZuK: How do you mean?
Like this:
Code:
```    //Check if you hit the square
int &n;
n = sXl;
if (sXl == pXr || sXr == pXl)
{
//New position for the square
n = GetRand(0, 640);

SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );
apply_surface( 0, 0, background, screen );
apply_surface( pXl, pYl, piece, screen );
}

//Apply the square
apply_surface( n, sYl, square, screen );```
Hussain Hani: Fixed

6. Changed some of my code...

Code:
```    //Check if you hit the square
int n = sXl;
if ((pXr >= sXl && pXr < sXr) || (pXl <= sXr && pXl > sXl) || (sXl >= pXl && sXl <= pXr) || (sXr <= pXr && sXr >= pXl))
{
//New position for the square
n = GetRand(0, 640);

SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );
apply_surface( 0, 0, background, screen );
apply_surface( pXl, pYl, piece, screen );
}

//Apply the square
apply_surface( n, sYl, square, screen );
}```
Same problem!

7. So why not try debugging it then?

Put a breakpoint on the if() statement, then examine the variables.

Decide whether the if() should be true or false based on those values, then single-step the code.

If at any time the code does something different to what you expect, then you've found the bug.

8. Originally Posted by Livijn
ZuK: How do you mean?
You said that on collision you want to move the square away a random distance. If you do that then the calling function has to know about that, otherwise when you call check_coll() the next time the square will move back to the original position. That is why I said that you have to make the position of the square a reference parameter to be able to change the position in the calling function.
Kurt

9. Yeah but how do i do that?

10. Originally Posted by Salem
So why not try debugging it then?

Put a breakpoint on the if() statement, then examine the variables.

Decide whether the if() should be true or false based on those values, then single-step the code.

If at any time the code does something different to what you expect, then you've found the bug.
There's no code error. I just don't know how to do this...

11. Then read your compiler documentation and experiment with very simple programming scenarios. Familiarity, if not proficiency, with your debugger/tools in general is pretty much a fundamental necessity.

As a quick test you can always try putting in some code to write variable values to file or otherwise display them.

12. I'm not good at english. What do you mean?

13. Code:
`std::cout "This is a debug statement showing that variable whatever is: " << whatever << std::endl;`
Add these kind of lines in your code to view the status of variables.

The same idea can be applied to checking where your program branches.

If you don't like this approach, figure out how to use a debugger.

14. > There's no code error. I just don't know how to do this...
So what's the question, if it already works properly?

15. Im responding to your PM here.
What is the problem with the code you have? Is there something wrong with the collision detection itself or is it only the response that causes problems? Did you look at the code I posted before?

This might just be me, but I find your code hard to read with those variable name.

good luck