I'm trying to put together the physics of a classic platform game in Pico-8. I want to achieve something that behaves like Super Mario Bros.
I have coded a jump system where when the player presses jump, the character rises by a set number of pixels each frame, with that number decreasing every frame, so the character slows down as he gets to the top of his jump. This works well, but I run into problems when I try to implement a variable jump. Like Mario, I'd like my character's jump height to depend on how long the jump button is held down. I did achieve this simply by cancelling the vertical climb when the button is released, but this means that the jump doesn't slow to a halt unless the button is held long enough to see the full jump.
I suspect I'm going about the whole thing in the wrong way and wondered if someone can tell me how the guys at Nintendo did it with Mario. I'm sure there are lots of ways of achieving something similar, but I'd like to know how it was achieved on the NES if possible as I'm as interested in the technique as I am in the end result.
Some Googling suggests that the best method to use is by setting up forces acting on the character. Is this how it would have been accomplished back in the 1980s?
Thanks for any help! :)
I don't know exactly how Mario's jump was done, but I imagine there are about 3 variables involved.
jump_height -> How long the button has been held, we need to track this so we can stop the user from jumping once they reach the highest possible height.
y_force -> gravity basically. The amount of pixels to add to the Y position of the character every update.
jump_allowed -> a flag to disabled further jumping if the jump button is released or if they reach the highest they can jump.
Mario appears to have a constant jumping speed while the jump is held down until he reaches the highest point of the jump at which he starts being affected by gravity, so I'd say do something like this (and this is sudo code since I haven't touched pico in a while)
init() jump_height = 0 y_force = 0 jump_allowed = true end update() if jump is held and jump_height < 10 and jump_allowed then y_force = -3 jump_height += 1 end if jump_height == 10 or not jump_allowed jump_allowed = false jump_height = 0 y_force += 1 --To have the player start falling end if player touches the ground jump_allowed = true y_force = 0 end player_y += y_force end
Hunted down an exhaustive analysis of mario physics. For some reason it was in image format, so I'll just restate the the pertinent part:
Current run speed determines initial velocity (and slightly tweaks gravity). When you jump, holding A reduces gravity as long as vertical speed is upwards.
(Also no air friction and caps for horz and vert speed.)
(values in pixels, approximately) [b]run jump A full speed vel grav grav[/b] 1 4 -2 -7 2.3 4 -1.9 -6 2.6 5 -2.5 -9
Those are all great bits of advice and resources, thanks guys!
I have managed to get something set up with acceleration in x and y directions, with gravity, friction, variable jumps and of course floor detection. I'm going to try to get jumps to take into account the run speed as described above and then I'll stick my efforts up on the forum to see what people think.
[Please log in to post a comment]