Collision Detection Code Structure with Sloped Tiles

Posted by ProgrammerGuy123 on Game Development See other posts from Game Development or by ProgrammerGuy123
Published on 2013-10-27T18:11:53Z Indexed on 2013/10/27 22:02 UTC
Read the original article Hit count: 493

Im making a 2D tile based game with slopes, and I need help on the collision detection. This question is not about determining the vertical position of the player given the horizontal position when on a slope, but rather the structure of the code.

Here is my pseudocode for the collision detection:

void Player::handleTileCollisions()
    {
        int left = //find tile that's left of player
        int right = //find tile that's right of player
        int top = //find tile that's above player
        int bottom = //find tile that's below player

        for(int x = left; x <= right; x++)
        {
            for(int y = top; y <= bottom; y++)
            {
                switch(getTileType(x, y))
                {
                    case 1: //solid tile
                    {
                        //resolve collisions
                        break;
                    }
                    case 2: //sloped tile
                    {
                        //resolve collisions
                        break;
                    }
                    default: //air tile or whatever else
                        break;
                }
            }
        }
    }

When the player is on a sloped tile, he is actually inside the tile itself horizontally, that way the player doesn't look like he is floating. This creates a problem because when there is a sloped tile next to a solid square tile, the player can't move passed it because this algorithm resolves any collisions with the solid tile. Here is a gif showing this problem:

Collision Problem

So what is a good way to structure my code so that when the player is inside a sloped tile, solid tiles get ignored?

© Game Development or respective owner

Related posts about collision-detection

Related posts about code-structure