2D Platform Jumping\Collisions

BlitzMax Forums/BlitzMax Beginners Area/2D Platform Jumping\Collisions

QuickSilva(Posted 2009) [#1]
Can anyone offer me some advice on how to tackle 2D Mario style platform jumping? It`s actually a little harder than it seems.

After a look around on the web and several failed attempts myself I`m a bit baffled as to how to tackle the platform collisions successfully.

I have a character who can jump at variable heights depending on how long the jump button is held down and that works fine, the problem is that when he falls back down to earth, because he is effected by gravity, he ends up going through my platforms by a different amount each time. I need him to obviously land on top of these platforms. I have tried several approaches including resetting his y position to the point before he hit the platform and also moving him back up from the platform once he has hit until he no longer hits it but neither seemed to work that well in practice.

Are there any other approaches that I could look into?

Any help would be most appreciated.

Jason.


Jesse(Posted 2009) [#2]
I helped Amon with his code a few months back with his tile platform game.
and he posted the working code a few days back here:
http://www.blitzmax.com/Community/posts.php?topic=84117

if you can't figure it out from that code let me know and I I'll be glad to help you solve it.


QuickSilva(Posted 2009) [#3]
Thanks :) I`ll have a read and get back to you if I need more help.

[Edit]
OK, I`ve had a look at it but am still having trouble understanding what is happening when the player is falling with regards to the Mod part. Could you please take a moment to describe it to me in pseudo code?

Seems to work well though so hopefully with a little more help I will be able to adapt it to my needs.

Jason.


Jesse(Posted 2009) [#4]
I don't know if you know what mod is, so I am going to explaint it. mod gives you the remainder of a division. example 7.5 mod 4 = 3.5 now lets say you tiles are 32x32 and the botom of your character is y. If the character is at say 96y which is at the bottom of the tile. If you calculate the remainder of 96 mod 32(tile height) you get 0 which we assume is where you want to be at y when the character lands. if say the player is at 99, 99 mod 32 is 3 that sets the character 3 pixels below the edge of the tile. if it is a small number of pixels of the edge of the tile you want it to return to the edge else you want it to keep going down.
I am going to give you an example assuming you are using a two dimensional grid, the character is on its way down and the character is one pixel wide.
if [x/32] ,y/32] = 1 then  ' detect if there is a tile at the specified location 32 is the tile size
    if (y mod 32) < 4 then ' detect if the character is at most 4 pixels below the top of the tile 
       y = floor(y /32)*32 ' set it back to the top of the tile
       jump = false        ' stop the character from jumping
    endif
endif

you need be aware that if the falling speed is greater than 4 then it's going to fail the test randomly and it will possibly keep falling and go through all of the tiles with out stopping. you either need to set the speed limit to 4 or compensate for the jumping speed.

also what I did is I checked one point against a tile but characters are generaly more than one pixel wide so You need to check the front and back of the character in case only one of the two ends is touching the tile otherwise it's going to keep on falling. Hense Amon's example:
		If Map[(PlayerX + Player_Width) / 32, PlayerY / 32 + 1]= 1 or Map[(PlayerX - Player_Width * 2) / 32 + 1, PlayerY / 32 + 1]= 1
		

what I showed you is just one way. you can also do a pixel perfect collision like Dabhand example, and others. There are several tutorials in the internet I even saw one in Youtube a while back. I don't know if it's there anymore.
well, I hope you understood that.


QuickSilva(Posted 2009) [#5]
Thanks for the explanation. I`m going to give it a go and see how it works.
Just out of interest would this approach work for non tile based platformers?

Thanks again,
Jason.