I'm currently developing a 2D platform game and have almost everything in place, but the collision detection is really taking some time, basically I can detect the collision happening between 2 objects using a intersecting rectangles function
bool Game::CheckCollision(GameObject p1, GameObject p2)
{
int leftp1, leftp2;
int rightp1, rightp2;
int topp1, topp2;
int bottomp1, bottomp2;
leftp1 = p1.m_x;
rightp1 = p1.m_x + p1.m_width;
topp1 = p1.m_y;
bottomp1 = p1.m_y + p1.m_height;
leftp2 = p2.m_x;
rightp2 = p2.m_x + p2.m_width;
topp2 = p2.m_y;
bottomp2 = p2.m_y + p2.m_height;
//If any of the sides from A are outside of B
if( bottomp1 <= topp2 )
{
return false;
}
if(bottomp2 < topp1)
{
return false;
}
if(bottomp1 >= topp2 && rightp1 <= leftp2 && leftp1 >= rightp2)
{
return false;
}
if( topp1 >= bottomp2 )
{
return false;
}
if( rightp1 <= leftp2 )
{
return false;
}
if( leftp1 >= rightp2 )
{
return false;
}
//If none of the sides from A are outside B
return true;
}
this is basically the same as the tutorial on lazyfoo website.
so then I have the game loop check for collision between the platform and the player and if it returns true then set a bool in the player class to true
if(CheckCollision(platform, player))
{
player.Colliding = true;
}
then in the movement function it only moves if the object is not colliding
if(!Colliding)
{
m_x += m_newX;
}
if(!Colliding)
{
m_y += m_newY;
}
but as you can probably guess, now when the player is on top of a platform he cannot move left or right as it counts as a collision, i attempted trying to break up into sections like top collision, side collision and such but could not implement it properly. Anyone have any ideas of how to do this?

New Topic/Question
Reply



MultiQuote








|