0
\$\begingroup\$

I am trying my hand at making a small 2D platformer. Currently I am trying to implement collision on a tile map. I have been trying to experiment with different ideas but can not find a way for the life of me. Currently, bounding boxes have been set up around the player and the blocks.

My current algorithm is as follows:

        for (int i = 0; i < blockBound.GetLength(0); i++)
        {
            if (playerBound.Intersects(blockBound[i]))
            {

                if (playerBound.Bottom > blockBound[i].Top)
                {
                    jumping = false;
                    player.pos.Y = player.prevPos.Y;
                }
            }
        }

I have tried using an intersect amount by subtracting the blockBound[i].Top value from the playerBound.Bottom value and moving the player by that much back to their previous position but it only caused the player to move in and out of the block rapidly.

When doing the same thing with all sides, you could probably guess, always makes the collision code run.

Also with this code, when the player jumps, it appears to have somewhat of a jolt for lack of a better word when it begins jumping, compared to the normal smooth jump it has without any collision.

Is there a better way of doing this and if so, how?

\$\endgroup\$
1
\$\begingroup\$

You could use the Separating Axis Theorem, for which there are many excellent tutorials out there. The one that I still remember being very helpful was the one made by the team of the game "N":

Part 1: Collision between two objects

Part 2: Tile grids

It features interactive elements to play around with, making it really easy to understand the process intuitively.

Also, if you are having only rectangles as colliding objects, you can skip the part with round shapes. Although you probably want the lower end of your game character to be something like a circle, to avoid getting stuck at places, where one floor tile ends and the next one starts.

\$\endgroup\$
  • \$\begingroup\$ I have lots of pages to check out. I'll be sure to look at these. \$\endgroup\$ – user220631 Aug 20 '16 at 23:05
0
\$\begingroup\$

I suggest you to check out this sample game on a simple platformer game. It has an example of one side collision as well.

MonoGame.Samples Platformer2D on Github

Hope it helps!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.