0
votes

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.

1

1 Answers

1
votes

If the problem is that your player is being updated before all the collisions are detected, then try batching the process. That is; instead of applying the vectorTranslations immediately create a list of them, then when all collisions have been detected apply the list of translations to the player.