How to make a gravity-based platformer

There has been a bit of interest and intrigue about how I made the gravity mechanics in AirScape. So I have decided to write my first tutorial on how I did it, and show how simple it really is.

The first thing I must say, is that this method was conceived by Yann Granjon. I did not come up with the idea, I simply implemented it. He helped very much through the production of the game and it would not have been the same without him!

The basics

The most important thing to consider for this to work is the ability of the Platform behavior to change gravity angle. Once we know this works, the important thing to consider is, how will the game know which gravity direction to set at which point?

So Yann suggested, and I implemented, gravity zones.

(Sorry for blurry pictures, they seem to have lost quality on upload)

Here you can see an overview of level 1-1. The main thing we are interested in, is the colored zones surrounding the landmass.

These shapes are all frames of the same object, 'gravzone'. There are about 5 shapes, all for different purposes.

There are two types of gravzone. "Relative", and "Non-Relative." This is just decided by a boolean.

The code

The code is very simple. Here are the basics:

Basically, there is an angle for any gravzone. This angle is determined based on whether or not the gravzone is relative.

Generally, the rounded gravzones are relative, and the straight ones are not. The difference is that the relative gravzones attract towards a single point, which allows rounded surfaces, whereas non-rounded gravzones only attract towards a specified angle (this angle is usually just specified by the angle of the gravzone itself.)

So anyway, back to the code. All it does is, for each gravzone the player is overlapping, determine the correct angle (determined by gravzone.a for non-relative, or angle(player.x,player.y,gravzone.x,gravzone.y) for relative) and then turns this angle into two vectors. It does this so that the vectors can be ADDED to the previous vectors, and we end up with a vector pointing in the average direction of the angles specified by all gravzones the player is overlapping at that moment.

This is so that I can overlap two or more gravzones, and end up pulled towards the average of their angles.

Flipping 180

There comes a small problem when flipping 180 degrees. Basically, the average of the angles cancels out and the player falls in an awkward direction. So I integrated another invisible object, the flip_assist.

This overrides the traditional gravity programming while the player is overlapping the flip_assist.

This code basically switches the standard gravity angle for a gravity angle manually set by the flip-assist, and then lets it switch back to normal when the player is no longer overlapping one.

There are actually two flip_assists for every yellow bar you see, so the player can flip back and forth. This necessitates the 'cannot_reset_flip' boolean so the game doesn't get confused.

And that is pretty much it. With a bit of angle lerping:

We have the basic mechanics of the game.

Here are some examples of levels to get a better idea:

Hope you enjoyed the tutorial. Make sure you let me know if I missed anything!

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!