• Advertisement
Sign in to follow this  

Collision Detection HELP!

This topic is 1984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thats not right either ^^^ i dont know y it wont let me post the correct code???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.
[CODE]
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
}
[/CODE]

Share this post


Link to post
Share on other sites
You need to compare the top of one to the bottom of another, not the bottom with the bottom.

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement