# Check collision

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-11-2007
Livijn
Check collision
Hi!

You guys won't get rid of me! :D I have some work to do with my game before it's done. I need to check if my piece hits the square. I don't want to mess around with anyone else's code. It will just become messy and i won't understand it.

I tested to do like this, just for a test, this is not all checks i'm gonna do, but it should work if my piece comes from the left and hits the square.

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;         if (sXl == pXr)     {             //Apply the square             apply_surface( 400, 100, square, screen );     }                 }```
Why aint this working? :(
• 05-11-2007
Daved
With the sizes of the squares and pieces being different, wouldn't it be better to test if any part of the piece was in any part of the square, rather than comparing the left edge of the square with the right edge of the piece for equality? Is it possible for the piece to move past the edge of the square?
• 05-11-2007
Livijn
How would i do that? To test if any part of the piece was in any part of the square? :s Confused
• 05-11-2007
Daved
I don't know what the best way would be, but you can just figure it out. For example, if the right edge of the piece is between the left and right edge of the square, and the bottom edge of the piece is between the top and bottom edges of the square, then you know it overlaps. You'd also have to check the other corners. If the piece is bigger than the square, you might have to check more positions (like perhaps the middle of the piece) in case it completely covers the square.

By the way, I don't know if this is your problem, I'm just guessing from your posted code. You have to figure out if this reason makes sense.
• 05-11-2007
Salem
Mmm, another collision checking post...
• 05-12-2007
Livijn
Actually, mine works. BUT i want it to erase the old sqaure forever. How?
I do like this and it erases every time the sXl is 400 and pXr is 400.

My function:
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;         if (sXl == 400)     {     if (sXl == pXr)     {             SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );             apply_surface( 0, 0, background, screen );             piece = load_image( "piece.png" );             apply_surface( pXl, pYl, piece, screen );                         sXl = 100;             //Apply the square             apply_surface( sXl, sYl, square, screen );     }     if (sXl == pXr)     {             sXl = 100;             //Apply the square             apply_surface( sXl, sYl, square, screen );     }     }         if (sXl == 100)     {     if (sXl == pXr)     {             sXl = 340;             //Apply the square             apply_surface( sXl, sYl, square, screen );     }     if (sXl == pXr)     {             sXl = 340;             //Apply the square             apply_surface( sXl, sYl, square, screen );     }     }                         }```
Soe code in main:
Code:

```            //Apply the square             apply_surface( sX, sY, square, screen );                         //Check collision and move the square             check_coll(sX, sY, x, y);```
• 05-12-2007
Livijn
Bump
• 05-12-2007
Salem
Any more will result in thread closure.
• 05-12-2007
Livijn
• 05-12-2007
UMR_Student
Actually, I for one find it annoying that you bump your threads expecting us to spoonfeed you a solution to a problem that isn't really that challenging and that you could probably figure out if you read your own code and thought about it for a few minutes.

>Actually, mine works. BUT i want it to erase the old sqaure forever. How?
>I do like this and it erases every time the sXl is 400 and pXr is 400.

Here's a clue, it probably has to do with where you put these two lines:
Code:

```SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) ); apply_surface( 0, 0, background, screen );```
• 05-12-2007
Livijn
Well, that wasn't my question actually. If it was, i would have solved it for years ago. :P No, my problem is almost the same as i said, BUT it wont disapeare. If my position is the same as the squares, it works but when i move from the old square's postion, the new square shows up and the old disapeares.

And also, now i have added a random function. It works, it's just that the postion of the square moves constantly. I wan't it to stop after 1 random choice.

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() &#37; (max - min + 1) + min);     return (rc); } //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;         //New position for the square     int n;     n = GetRand(0, 640);     if (sXl == pXr || sXr == pXl)     {             SDL_FillRect( screen, NULL, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00) );             apply_surface( 0, 0, background, screen );             piece = load_image( "piece.png" );             apply_surface( pXl, pYl, piece, screen );                         //Apply the square             apply_surface( n, sYl, square, screen );     } }```
• 05-12-2007
Daved