hyperlogic.org
A witty saying proves nothing. -Voltaire

Jump Tuning

I was flipping through an old Game Developer magazine and found an interesting article about jump physics. (Nov 2009, Inner Product: by Mick West). It's a good article, It made me want to share an insight on character jump tuning that I've gained while working on various games.

Jumps can be represented as a function of time.

x(t) = v_x * t
y(t) = v_y * t + (g * t^2) /2

x(t) is the horizontal component and y(t) is the vertical component of the jump. In our coordinate system, x is forward and y is up. So that means vx and vy should be positive and g should be negative.

As a programmer, it's very easy for me to expose vx, vy and g as parameters for each kind of jump in the game.

  • vx - The initial horizontal speed.
  • vy - The initial vertical speed
  • g - Gravity (-9.8 m/s2 is real-world gravity).

The game designers can then adjust these values to tune the jump to their liking. The problem is, designers don't really know what the initial speeds and gravity should be to get the effect they want. All they really care about are answering the following questions:

  • How far can the character jump? So, they can make gaps that can't be jumped across.
  • How high can the character jump? So, they can make obstacles that can just barely be jumped over.
  • How long does a jump take? So, they know what the response time is between when the player hits the jump button and when they regain control on the ground.

Oh, yeah and they'll want to change the answer to each one of these questions without effecting the others.

What we have here, is a typical case of: Easy for the programmer, hard for the designer.

Picture of a Eddie Deezen

A much more convenient set of tuning parameters would be:

  • h - jump height
  • a - hang time
  • d - jump distance

These three make a good choice because they are orthogonal. i.e. Making a change to hang time does not affect the height of the jump or the jump distance.

So now, how do we translate these numbers into parameters we can plug into our motion equation?

The jump height (h) is the distance from the ground at the apex of the jump. The apex is the point in the path where the vertical velocity is zero. i.e. the point at which the character hangs in mid-air. So if we can find this apex, we can figure out the jump height.

This is our vertical function of motion.

y(t) = v_y * t + (g * t^2) /2

If we differentiate it with respect to time, we get a new function y'(t) which represents the instantaneous vertical speed of the character at time t.

y'(t) = vy + g * t

Set this equal to zero, and solve for t to get:

t_a = -v_y / g

This ta is the time the character hangs in mid air. So if we plug this time into our motion equation we'll get the height at the apex. i.e. the jump height (h) we're looking for.

y(t_a) = y(\frac{-v_y}{g}) = v_y(\frac{-v_y}{g}) + \frac{g(\frac{-v_y}{g})^2}{2}

Simplifying:

h = \frac{-v_y^2}{2g}

Now lets figure out the hang time of the jump. The apex of the jump occurs half-way through the jump, so it stands to reason that the hang time is just twice the apex time. This works because the parabola is symmetric about its apex.

a = 2t_a = \frac{-2v_y}{g}

We're assuming the character is jumping on flat ground, so, the hang time (a) is when the character lands. If we plug in the hang time (a) into the horizontal motion equation we'll get the final jump distance d.

x(a) = v_xa = v_x(\frac{-2v_y}{g})

d = \frac{-2v_xv_y}{g}

Ok, now we have 3 equations:

h = \frac{-v_y^2}{2g}, a = \frac{-2v_y}{g}, d = \frac{-2v_xv_y}{g}

If we take the first two equations and solve them as a system (two equations, two unknowns), we get:

g = \frac{-8h}{a^2}

and

v_y = \frac{4h}{a}

if we plug those into the third equation we get:

v_x = \frac{d}{a}

Now we have a way of computing what our initial velocity (vx, vy) and gravity (g) should be in order to get the motion described by h, a and d.

v_x = \frac{d}{a}, v_y = \frac{4h}{a}, g = \frac{-8h}{a^2}

We're done. We've made the designers happy by giving them a set of orthogonal controls that map directly to the things they care about most.

Well, until they ask for air steering and double jumps that is.