# 2D platform collision

I have went over and over in my head for the past month trying to get a simple platformer together. My problem is collision. So far from every tutorial I've found this tilemap based collision needs to be done this way: Check for for tile in x+xVel / tilesize if so xVel=0 same for x axis just subtracting for when the player goes left. Check for tile in y+yVel/tilesize. if so yVel=0. No matter how many different ways I end up programming this concept. The player always ends up clipping walls, falling through, or just hanging out 2 pixels before the collision. Can anyone tell me what's up?

By the way my collision check is a two point method. eg. Check top two points of the rectangle box if going up, bottom two for down ect.

Thanks!

• It might help to post some code, but the main problem is likely due to timestep. If you velocity along the x axis is 5, and you have a distance of 3 until the wall, your code to detect an obstacle will detect this and stop the character. You could, for example, detect this and only move the character 3 units rather than the full 5 units. Jul 3, 2015 at 15:58

This is a great tutorial on tilemap collision, but you can also use Box2d. That is what I ended up doing because tilemap based collision only works if your character moves in increments of 1 tile at a time. If he can move fractions of a tile it doesn't really work.

• "tilemap based collision only works if your character moves in increments of 1 tile at a time." I don't see how that's valid. Old console games used this method perfectly fine without floats or any complicated things. SMB1 collision would be great for what I'm making. Jul 3, 2015 at 17:32
• Hmmm maybe I misunderstand what you mean then. Look at the video. If that is the kind of collision detection you want then that only works if he moves one tile at a time. If you want him to move freely, then the only easy way I know of would be to use box2d. It is quite simple to use. That's what I ended up using after I couldn't get the other kind of collision detection working Jul 3, 2015 at 17:41

Your player has two points for each direction, so 8 points in total. For each point do the following:

1. Check where the point is, where it wants to go. I use a line draw algorithm to determine the tiles the point crosses.
1. Pick the nearest collidable tile from the tiles you found.
2. If a collidable tile is found: stop movement to that direction.
3. The important one: calculate the position the player would end up. This is relative easy as you know the tile that caused the collision. Example if the collision was moving left, the position would be something like: collidedtile.x-collidedtile.width

Note that things can be optimised to verify if the point has to be checked (example only check horizontal collisions if the character moves that direction). I also use a switch statement for each 4 directions so I can add additional checks (for example jump-through-platforms in the up direction check) or events (character has landed a jump in the down direction).