• entries
18
8
• views
14,649

# How to Jump

568 views

HOW TO JUMP
Ever fired up a platformer, and thought "Damn, this looks great! But why does the player hover when I jump??"

It's a very common thing for developers to not really understand how jumping works, and to simply code a fixed speed up for a few frames, then a fixed speed down. The net effect is a very unnatural motion, and if the player is falling, they look more like they are levitating. (REALLY lazy ones might just pop the character into the air for a few frames, then pop them back down. That looks more like an old LCD game.)

It's a shame, because simulating a realistic jump is not only very easy to do, it adds greatly to the playability of your game. The motion feels more natural and the player will be able to instinctively predict the motion.

In real life, all things are affected by the pull of gravity. But what is gravity? Nobody actually knows. But the EFFECT is very predictable -- a constant acceleration towards mass. In the case of standing on planet Earth, the average effect is an acceleration of 9.8m/s^2 towards the center of the planet. (Of course, if you are standing on something and not falling, the acceleration is blocked - but it's that force that keeps you stuck there.)

The word "acceleration" is key. And this is really easy to put into code.

The really simple jump function probably looks something like this:

`int vertical_speed = 0;int jump_time = 0; ... if (jump_button_pressed) {  vertical_speed = -5;  // fixed upward motion  jump_time = 5;        // move up for 5 frames} else {  if (jump_time == 0) and (not_standing_on_ground) {    // not jumping, not on the ground, so we need to fall    // this would also be the down part of a jump    vertical_speed = 5; // fixed downward motion, no counter needed  }} else {  // not jumping, not falling, so no vertical movement  vertical_speed = 0;} // update the player vertical positionplayer_y_position += vertical_speed; // count down the jump_time if it's setif (jump_time) --jump_time;`

But we don't move like that in the real world. What happens when we jump? We tense our muscles, and push upwards as hard as we can. After that point, the acceleration of gravity does the rest - there is no "timer", the length of time we are in the air depends on how hard we start -- that is, our initial velocity. We can simulate that almost as simply as above, and get much more realistic movement.

`int vertical_speed = 0; ... if (jump_button_pressed) {  vertical_speed = -10;  // initial velocity - usually start higher than fixed motion as it will count down fast} else {  if (not_Standing_on_ground) {    // if not on the ground - then we are in the air, so gravity always affects,    // even immediately after starting to jump! Our gravity here is 1 pixel/frame^2    vertical_speed++;  } else {    // we are on the ground, so make sure we stop falling    vertical_speed=0;  }} // update the player vertical positionplayer_y_position += vertical_speed;`

See how simple that is? There are two things to note with the acceleration version.

The first is that you will need to tweak the vertical jump speed and maybe the acceleration to match your game and the movement you want.

The second is that you will very likely want a maximum vertical speed (terminal velocity), if only to ensure you don't miss the ground when you finally hit it.

(I was going to make this a video, but I couldn't find the example programs I wrote years ago... so a text post it is!)

There are no comments to display.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.