1
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 3, 2015 at 15:58

2 Answers 2

1
\$\begingroup\$

https://www.youtube.com/watch?v=DOpqkaX9844

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.

\$\endgroup\$
2
  • \$\begingroup\$ "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. \$\endgroup\$
    – hobbyguy54
    Jul 3, 2015 at 17:32
  • \$\begingroup\$ 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 \$\endgroup\$ Jul 3, 2015 at 17:41
0
\$\begingroup\$

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).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .