Hello again, Code Ninjas!
Today is something very simple: a formula that allows you to plug in variables for "jump force" and "gravity" and get the maximum height of a jump as the result.
When designing platformers, tweaking the physics until they're just right is very important. Having a formula such as this will be useful for zeroing in on exactly what values constants like gravity should have.
Another use might be this: say you already have established physics, for instance because you're hacking a Sonic game. But you want to add a new object, such as a new kind of bumper or spring, that bounces Sonic exactly 5 times his height. Determining the force at which Sonic should be impelled by the object in order to achieve that height would be a snap using the following formula.
The simplest way to write the formula is this:
g
is your gravity, and f
is the jump force, which can be set to anything. In a Genesis Sonic game, for example, gravity is 0.21875pps (pixels per step) and the jump force is 6.5pps.
h
is the height value we are trying to find out - the number of pixels the character will travel given the jump force f
with gravity g
. t
is time, which is optional - it will be the number of steps it takes to reach the apex of the jump. Both h
and t
are initialised at 0.
Then we run a loop while f
is greater than 0. In the loop, first f
is added to h
, then g
is subtracted from f
. This simulates the jump: the force acts upon the character's position, then the gravity acts upon the force. t
is then incremented by 1 in order to count the time.
When the loop is finished, h
will be the ultimate height of the jump.
Note that this code assumes your physics to handle similarly to Sonic's. In Sonic, because of the particulars of the original code, the jump force is added once to the character's position before gravity acts upon it. If this is not the case in your game, then the loop should be restructured accordingly; g
should be subtracted from f
before f
is added to h
.
Earlier I said "the simplest way" to write the formula. The truth is that this, while simple, is a rather brute force method. Depending on the strength of the jump force and gravity, the loop could run dozens of times. Yes, modern computers can handle this without breaking a sweat, and yes, you probably won't even be using the formula in a running game, anyway. But for the sake of mathematical beauty, we can find a better way that doesn't employ a loop.
So let's build this new formula piece by piece. First, we need to find how many steps it will take for gravity to whittle the jump force away to 0 (or less). This will be time t
again.
We find t
by dividing f
by g
and rounding up to the nearest 1. Why the rounding up? Well, if the jump force isn't perfectly divisible by gravity, the remainder would still count as upward velocity and the player would still move up by a little bit during that step. Since it counts toward the total, it must be taken into account.
Now that we know how long the jump will take to reach the apex, we can easily discover the distance the character would travel during that time, without gravity, merely by multiplying f
by t
.
Of course this seems a little silly, because we're trying to find the height covered with gravity taken into account. But knowing this value is useful; 'cos if we can also determine how much force is deducted by gravity from the jump force over t
steps, we can multiply gravity by that number, subtract it from h
, and have our correct result.
In the first step (t
= 0), the jump force is unaffected. In the next, it is lesser by gravity. In the next, it is lesser by gravity again, i.e. it is equal to the initial jump force value minus gravity times 2. Next step, times 3, then in the next, times 4, and so on, until gravity overcomes the jump force in step t
minus 1.
Visually represented, you might think of the amount of force lost to gravity as a triangular stack like this:
----- (t=0) g---- (t=1) gg--- (t=2) ggg-- (t=3) gggg- (t=4) ggggg (t=5) ...
Fortunately it is easy to find the area of a triangle such as this by finding the area of a square the size of t
times t-1
and then cutting that value in half. (If, as mentioned above, your physics subtract gravity before the character moves once, the square should have a size of t
times t+1
instead).
Et voilĂ , you have the correct height of the jump, identical to the result of the while
statement method used above.
Finally, because multiplication is a commutative process, the formula can be recast in a simpler way for our final code:
You can find a small example .gmk here that lets you play with the variables to get different jump heights. Until next time, happy coding!