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]
6 replies to this topic
Sponsor:
#3 Members - Reputation: 124
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]
[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.
#5 Members - Reputation: 199
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.
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.
#6 Members - Reputation: 131
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
}
#7 Members - Reputation: 1614
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:
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 Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)






