• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Collision Detection HELP!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Ars7c3  Members

Posted 14 August 2012 - 05:35 PM

Okay, so i have been working on a pong game in order to test my very basic game engine, and everything that the engine is supposed to do it's doing. The problem is in the collision code, which is confusing considering I have used this very same code successfully in other games I have made. I honestly have no idea what the issue is, and I was hoping one of you guys could help me. I don't know if this would make a difference or not but I am using Dev C++.

Here is the collision code and how it is used (BTW I made sure the SDL_Rect coordinates are correct)

[source lang="cpp"]bool Collision(SDL_Rect A, SDL_Rect B){ //The sides of the rectangles int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; //Calculate the sides of rect A leftA = A.x; rightA = A.x + A.w; topA = A.y; bottomA = A.y + A.h; //Calculate the sides of rect B leftB = B.x; rightB = B.x + B.w; topB = B.y; bottomB = B.y + B.h; //If any of the sides from A are outside of B if( bottomA <= topB ) { return false; } else if( topA >= bottomB ) { return false; } else if( rightA <= leftB ) { return false; } else if( leftA >= rightB ) { return false; } //If none of the sides from A are outside B else return true;}[/source]

And here is how I call it in the main game loop:
[source lang="cpp"] if(Collision(paddle2.GetRect(),ball.GetRect())) { cout << "Hit" << endl; } [/source]

### #2Infernal-rk  Members

Posted 14 August 2012 - 07:28 PM

you are using assignment in your if tests...line 22 and 27.

You've also neglected to test outside the top and left.

please look up axis aligned bounding box collision.

### #3Ars7c3  Members

Posted 14 August 2012 - 07:33 PM

I actually did include that in my code... for some reason it didn't upload that way. Here is the real code i use to detect collision.
[source lang="cpp"]bool Collision(SDL_Rect A, SDL_Rect B){ //The sides of the rectangles int leftA, leftB; int rightA, rightB; int topA, topB; int bottomA, bottomB; //Calculate the sides of rect A leftA = A.x; rightA = A.x + A.w; topA = A.y; bottomA = A.y + A.h; //Calculate the sides of rect B leftB = B.x; rightB = B.x + B.w; topB = B.y; bottomB = B.y + B.h; //If any of the sides from A are outside of B if( bottomA <= topB ) { return false; } else if( topA >= bottomB ) { return false; } else if( rightA <= leftB ) { return false; } else if( leftA >= rightB ) { return false; } //If none of the sides from A are outside B else return true;}[/source]

Edited by Ars7c3, 14 August 2012 - 07:33 PM.

### #4Ars7c3  Members

Posted 14 August 2012 - 07:34 PM

Thats not right either ^^^ i dont know y it wont let me post the correct code???

### #5Miklas  Members

Posted 15 August 2012 - 08:36 AM

Use a third party place to paste your code, such as http://ideone.com/ or http://pastebin.com/

The best hint I can give without the full code is try to use the debugger; in your main don't run the game, only test the function with different values and see what value that fails the test.

### #6dEgle  Members

Posted 15 August 2012 - 11:56 AM

Hello! Here is my function that i use in my projects. It's just a little more compact and 100% working Hope it will help you.
int check_collision(SDL_Rect *a , SDL_Rect *b)
{
if(b->x + b->w < a->x) return 0; //just checking if their
if(b->x > a->x + a->w) return 0; //bounding boxes even touch
if(b->y + b->h < a->y) return 0;
if(b->y > a->y + a->h) return 0;
return 1;    //bounding boxes intersect
}

### #7BeerNutts  Members

Posted 15 August 2012 - 12:36 PM

You need to compare the top of one to the bottom of another, not the bottom with the bottom.

Like this:
if (topA > bottomB) return false;
if (bottomA < topB) return false;
if (leftA > rightB) return false;
if (rightA < leftB) return false;
return true;

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.