I am making a small 2D engine and decided to use separating axis theorem for practice. I have the collisions detection and translation working with a player object, but when the playing collides with more than one object it will stop the player's movement. Here is an example of my problem:
The player is moving along the wall pushing himself both down and right. Once the player reaches the intersection between two tiles he can no longer move down because the tile below him detects the collision and will push the player up before the tile directly to his right pushes him to the left. I believe the problem is that the tile below the player is being tested for collision before the other tile, but I don't know how to work around this.
Here is the portion of my code which I believe needs to be changed:
protected override void Update(GameTime gameTime)
{
gameCamera.Update(new Vector2(player._boundingBox.X, player._boundingBox.Y));
foreach (Layer l in currentLevel.Layers)
{
foreach (GameObject gameObject in l.layerMapObjects)
{
gameObject.Update(gameTime);
}
}
player.Update(gameTime);
foreach (Layer l in currentLevel.Layers)
{
foreach (GameObject gameObject in l.layerMapObjects)
{
if (gameObject._isCollidable)
{
Vector2 vectorTranslation = Collision(player._boundingBox, gameObject._boundingBox);
player._boundingBox.X += (int)vectorTranslation.X;
player._boundingBox.Y += (int)vectorTranslation.Y;
}
}
}
base.Update(gameTime);
}
All of my collision right now is between rectangles, and the Collision() function will detect collisions and return the translation needed to move the player if there is one. I hope that someone can help me out with this. Thanks.