Gravity and jumping in platformers

Phizzy is here to talk about proper gravity stuffs. Now, I've seen people use a couple of different things that they dare to call 'gravity'. Here are some of the more retarded-looking methods I've seen...


The 'Running up a Triangle' method.


The 'Top o' the Trapezium' method.


The 'OH NO RHE SKY GRAVITY HAS ME OH WAIT NEVER MIND IT'S STOPPING KTHXBAI' method.

And here's something that it should look at least a little like:


The... 'Actual gravity-looking' method.


Let's take a look at how these methods would work.


The 'Running up a Triangle' method.

This is one of the more common 'ideas' of gravity. A likely option for n00bs and their allies.
It is often done by using a flag and a counter. User presses jump button, flag goes on. When the flag is on, the player is moved up constantly the same amount, and a counter is added to. When this timer reaches a certain amount, the flag turns off, and the user stops rising. This, combined with a 'flag is off' + 'player is not overlapping obstacle' - move player down event gives the crappy illusion of triangular gravity.
Now, try jumping.
Done it? Yeah? Did your speed stay consistent? Yeah? No? Explosion? No. You probably smoothly decelerated as you went up, then accelerated towards the ground. Unless you were wearing those awesome anti-gravity jumping boots I know you have. Those are so cool. Can I borrow them? Yes? No? Explosion!? Aww...



The 'Top o' the Trapezium' method.

This is rather bizarre, but I have seen it used on more than one occasion.
What happens is the player moves up with a consistent speed, then gets suspended in mid-air for a moment, then continues to move down with a consistent speed. Insane, no?
It's done by using two flags and a counter. When the user presses the jump button, the first flag is set on, and the second is set off. While the first flag is on and the second is off, the character is moved up and the counter is added to. When the counter reaches some arbitrary number or the character hits an obstacle above it, both flags are turned on, and the counter is reset. While both flags are on, the character is not moved vertically at all, and the counter is added to again. This time, when the counter reaches the end number, both flags are turned off. While both flags are off and the character is not on the ground, it should be moved down.
I think it's fair to say that it's obvious why this is not a veritable gravity effect... That's a lot of writing for some superfluous gravity method, though. ^_^

Onto some 'kinda almost right' method...


The 'OH NO THE SKY GRAVITY HAS ME OH WAIT NEVER MIND IT'S STOPPING KTHXBAI' method.

Okay, this is... Quite retarded, really, but it’s the most common method of gravity I’ve seen besides triangular gravity.
It works with two counters and a flag. When the player presses the jump button, turn the flag on. Then, make the events:

Flag is on --> Add 1 to Counter1.
Flag is on & Counter1 >= 3 & Counter2 > -5 --> Subtract 1 from Counter2 & Set Counter1 to 0.
Counter2 <> 0 --> Set Ypos to Ypos - Value(Counter2)

That’s clearly not proper syntax, by the way... Don’t try to copy and paste it or anything. You can change the Counter1 >=3 to a higher or lower number to change the ‘gravity’ strength.
Okay. Now, when the character is moving up far enough, he needs to invert his velocity to move back down, or some other silly words like that.

Flag is on & Counter1 >=3 & Counter2 = 5 --> Set flag off & Set Counter1 to 0

So now, you get sucked upwards until your velocity reaches a number, and then it is stopped, ready for the gravity to kick in...

Flag is off & Not overlapping backdrop --> Add 1 to Counter1
Flag is off & Counter1 >= 3 & Counter2 < 15 --> Add 1 to Counter2 & Set Counter1 to 0.

Okay! That does it alls.
As you can see, you get a weird Right-->Up curve, then an Up-->Right curve, followed by a Right-->Down curve with this method. If you try jumping again, you should be able to see that that’s not actually what happens to you under gravity.
That’s quite possibly the most complicated and least useful method... Now, onto the real thing! :O


The... ‘Actual Gravity-Looking’ method.

Finally, I get to write about something that doesn’t offend my eyes.
Right. Normal gravity should create a single curve trajectory when used this way. So... What will be needed for this is... Just the two counters.
To start with this, when the player presses the jump button, Set Counter1 to -9 & Set Counter2 to 0. Here, Counter1 will represent the vertical velocity variable, and Counter2 the delay in action of the gravity. Next, make an event that goes... Not overlapping backdrop --> Add 1 to Counter2. Now, for the gravity effect...

Not overlapping backdrop & Counter2 >= 3 & Counter1 < 15 --> Add 1 to Counter1 & Set Counter2 to 0.

This will make the vertical velocity rise when the gravity variable reaches a certain amount, and will not rise above 15 pixels/frame (terminal velocity). If you change these, you can alter the gravity strength and the terminal velocity of the character.

Finally, to make this code take effect, you need to make one more event...

Counter1 <> 0 --> Set Ypos to Ypos + Counter1

All of that should make a smooth, easily tweakable gravity effect.

And that’s all I need to say!


If you didn't understand any of that, here's a link showing all the different methods and how sucky some of them are:

Includes an example of a playable level with changeable gravity methods (can be used as a full platform movement tutorial), and a more theoretical frame that clearly shows the exact path taken with each method.

Example controls:

1 - 'Running up a Triangle' gravity method.
2 - 'Top o' the Trapezium' gravity method.
3 - 'OH NO THE SKY GRAVITY HAS ME OH WAIT NEVER MIND IT'S STOPPING KTHXBAI' gravity method.
4 - ‘Actual Gravity-Looking’ gravity method.

Left/Right - Walk (Game mode only).

Shift - Jump.