Hey, I'm having some trouble implementing collision detection with gravity in a simple mario-esque 2d platformer I'm making.
I have no problem detecting collisions, but it's how to deal with them that I'm struggling with. I can succesfully block tiles if I run into the left or right side of them. However when it comes to walking across tiles, and falling when none are there, I struggle.
I know I need some kind of 'gravity' velocity to constantly push on my character, and then I can simply react to it when a collision is detected. However, this doesn't seem to work, as I currently just do in the inverse of the velocity when a collision is detected. But this leads my character to becoming 'stuck' and unable to move, and the gravity then moves them down again in the next update...
My Collision Method:
void cCharacterPlayer::UpdateCollisions()
////Check collisions against tiles - all tiles for now - will improve
for (int row = 0; row < MAPSIZE_Y; row++)
{
for (int col = 0; col < MAPSIZE_X; col++)
{
//No point checking against tiles that aren't visible
if (m_gameState->GetMap()->GetTile(sVector2(col, row)).GetDrawableSet() == true )
{
//Intersect character rectangle with tile rectangle
if ( GetCharacterRectangle().Intersects(m_gameState->GetMap()->GetTile(sVector2(col, row)).GetCollisionRect()) == true )
{
//Gems
if (m_gameState->GetMap()->GetTile(sVector2(col, row)).GetType() == gem)
{
//Set player score
//Change to blank tile
m_gameState->GetMap()->GetTile(sVector2(col, row)).UnsetDrawable();
}
else
//Must be a solid tile
{
//Undo the last move
m_position.x -= m_velocity.x;
m_position.y -= m_velocity.y;
}
}
}
}
}
My rectangle checking:
bool sRectangle::Intersects(const sRectangle& _other) const
{
return !(left > _other.right || right < _other.left ||
top > _other.bottom || bottom < _other.top);
}
Totally stuck!
Thanks!
James