4
\$\begingroup\$

I am starting with game development and creating a basic platformer game. I already have searched a lot and found some ways to implement the platformer aspect to the game, like apply a gravity, velocity and so on.

Everything is good, but always when I implement a way to deal with the collisions, a bug appears. If I fix it, it appears at other side, etc. The main problem is when the player intersects a tile and I need push him up back. The most simple way that I found is this:

player.Y = tile.Y - player.height;

The most problems comes when you also need verify the X axis, the player is pushed out from the tile, or it is thrown at other tile position, like this issue I had when trying to implement the XNA Platformer Example. Or even pass through the tiles if you are going too fast.

After some research I found a good way to avoid all this problems: instead of move the player to the next position and deal with the tiles to push him back, calculate a safe distance and move the player following this. The "safe distance" is for example, the player.Y += player.Velocity.Y, but if any tile is at the way, the safe distance is decreased to the player don't enter in the tile.

I read this excelent article and it's what is said about the algoritm:

  1. The total movement of the player along that direction is then the minimum between the distance to closest obstacle, and the amount that you wanted to move in the first place.
  2. Move player to the new position. With this new position, step the other coordinate, if still not done.

So, the best way is use the "safe distance" method?

\$\endgroup\$
  • 6
    \$\begingroup\$ I would not use the word "best" because there is and can never be best method, only those that solve specific requirements - did the "safe distance method" solved all problems for you? \$\endgroup\$ – wondra Feb 14 '16 at 20:13
  • \$\begingroup\$ No, I didnt managed to implement the one way platforms \$\endgroup\$ – Rafael Almeida Feb 14 '16 at 20:27
  • \$\begingroup\$ First, you need to define "best". \$\endgroup\$ – Krythic Nov 30 '16 at 22:16
  • \$\begingroup\$ One approach is to cut the player's velocity in half and either move by that amount or don't, based on whether there's a collision, and then repeat this procedure (within the same frame) until the velocity is reduced to the position rounding increment. Since the velocity drops by half each time, the number of iterations shouldn't be particularly problematic. \$\endgroup\$ – supercat Apr 20 '18 at 22:00
0
\$\begingroup\$

Set their position to be almost perfect - account for floating point precision.

So if your player is 8 units tall, and your platform is at (5,6), and you land on the platform, then:

When you see an imminent collision, set the player's y position to 6+8+0.01

6 is the y position of the platform. 8 is the player size offset. 0.01 is the floating point precision correction.

This assume y grows upward, and the top left of the sprite is its location.

The correction for floating point precision is probably unnecessary, but depending on the implementation of your engine, it could be needed.

\$\endgroup\$
  • \$\begingroup\$ Is this better than resolve the collisions using the deep of rectangles? I already used this method but this fails in some test cases. \$\endgroup\$ – Rafael Almeida Feb 10 '16 at 19:22
  • 2
    \$\begingroup\$ Don't forget to set the velocity to zero for the direction the collision occured. I forgot that once and despite the collision being handles correctly the character still got stuck in the collided tiles. \$\endgroup\$ – Felsir May 11 '16 at 13:48

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.