Collision Detection HELP!

Started by
5 comments, last by BeerNutts 11 years, 8 months ago
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]
Advertisement
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.
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]
Thats not right either ^^^ i dont know y it wont let me post the correct code???
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.
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
}
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)

This topic is closed to new replies.

Advertisement