Platform Collision Fun

Monkey Forums/Monkey Programming/Platform Collision Fun

erebel55(Posted 2014) [#1]
I am working with therevill's platform code and am changing it for my needs.

I want to allow the player to jump through the bottom of tiles, which was easily accomplished by getting rid of some of the collision checks. However, the next thing I want is turning out to be more difficult.

I only want to check for a collision on the outside of the end tiles of a platform. Since the user can jump through the bottom of a platform I don't want to check for collision while the player is "inside" of the platform.

Here is a image of what I am trying to accomplish. This image represents a platform composed of 3 tiles.
The red sections represent where I want to check for collision and stop the player from moving through.


So the rules are..
1) Player walks on top of platform without falling through.
2) Player can jump through bottom of platform and reach the top.
3) If the player is jumping to the right he can't jump through the left side of the first tile in the platform.
4) If the player is jumping to the left he can't jump through the right side of the last tile in the platform.

Here is the modified code I am working with:


I was thinking about setting a flag for end tiles and then somehow only check to see if the player is hitting the outside x pixels of those. However, I was hoping someone else would have a more clever solution.

Thanks!


Gerry Quinn(Posted 2014) [#2]
It might help to first check the player's vertical position relative to the platform. If his feet are above the top of the platform, standard collisions apply. If not, check is he to the right of the platform, and if so check for a collision with the right edge. Same for the left.


ElectricBoogaloo(Posted 2014) [#3]
A tip for doing "Jumping through the platform" stuff..
Instead of changing your entire collision method based upon whereabouts the player is, instead think about the direction the player's moving. Essentially, test for collisions with the entire brick at all times, but only allow collision code to do anything when the player isn't actually moving upwards.

(Jay's silly hand test.. Hold out left hand, with right hand under it, both palm down.. bring up right hand, and note that the fingers curl down to allow the right hand to pass by the left hand.. next, bring the right hand back down, and note that the fingers don't bend, so the right hand slaps firmly against the left.. In both cases, the left hand collides with the right, but when moving up, the curling of the fingers allows passage, whilst on the way back down, there's a block. You didn't magically change the left hand's parameters, or even the right hand's parameters, in order to collide or not.)


zoqfotpik(Posted 2014) [#4]
The easiest way to do platformer code is to treat it as special cases. There isn't really an easy way to generalize it that I have found so just write it, make it perfect, encapsulate it and forget about it. For this there would be special cases based on whether the player is jumping or not. It might even be as simple as not checking for collisions when the player is moving upward. Then when he ceases to move upward, check for a tile collision and if he's inside a tile bump him toward the edge that is closest to his center.

Also for general interest there is a good article on platformer physics here:
http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/


erebel55(Posted 2014) [#5]
Gerry, right that is kind of how it is working right now. I check 3 positions on the bottom of the player to see if he is above a platform tile.

I also check 3 positions on the left side of the player when he is moving left and 3 positions on the right side of the player when he is moving right.
I am rendering dots at these positions to make it more obvious what is going on.


zoqfotpik(Posted 2014) [#6]
You could check the appropriate two corner pixels of a tile and see if either of them are inside the player boundingbox. Then count the horizontal neighbors of that tile and if there are two, disregard the collision.


erebel55(Posted 2014) [#7]
It looks like I can't get my head around writing that for some reason today :(
It sounds simple how you explained it zoqfotpik. I'm just having problems integrating that into the current collision code.
Will try to figure it out tomorrow.


erebel55(Posted 2014) [#8]
Here is my first attempt. I haven't added any safeties to make sure I'm not checking for neighbors that are outside of the level array yet.
Also, my code is pretty messy/repetitive/inefficient within the player update function. Specifically within the if clause at lines 150 and 229. If anyone has any ideas please share or feel free to touch up the code :)