Original Post
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]
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]