In modeling physical behaviors for the purposes of games, we try to get as close to a realistic model of the world as possible. Things like cars will have a velocity, acceleration, and friction, and even gravity will come into play if you catch air. Generally, we just apply all the forces acting on the car - acceleration from the engine, friction from the brakes, drag from the air, and gravity. Generally these are applied over long periods of time, and so modeling these all as forces and handing them off to the physics engine works very well.
However, jumping is not so simple. Unless we’re talking about rocket-boosted jumps, jumping is not a continuous force applied over time. Instead, it is an impulse - a powerful force with a very short duration, followed by ballistic motion until the jumper returns to earth. And for the purposes of a game, we may not want to think in terms of very large, short-lived forces - it would be preferable to think in terms of maximum jump height. That’s much easier to code with, and makes a level designer’s life a lot easier as well.
Let’s go through the physics to see how we can relate a maximum jump height to the impulse force necessary to produce it.
First, let’s start with the equation for ballistic motion - the motion that occurs after the jumper leaves the ground. We can ignore the horizontal motion for now; we’re only concerned with only the vertical component.
What we want to determine is an expression for the maximum height achieved during the jump. To that, we take the derivative with respect to time, and set it equal to zero:
We can plug this value of
Looks like the only variables governing jump height are the initial velocity, which is determined by the impulse we talked about earlier, and gravity, which is a constant. We can rearrange terms to get an expression for the initial velocity in terms of the jump height and gravity, which will be useful later on:
By convention,
Next, let’s figure out the impulse part. For this, we’ll want to use Newton’s Second Law of Motion. It is usually written as follows:
When dealing with impulses, however, it’s somtimes easier to write it differently:
What this says is that to achieve a change in velocity of
Apply this force to the jumper when it jumps, and the peak of its jump should be exactly
Why go to all this trouble of calculating a force when I could just set the velocity directly?
It’s certainly possible to do things that way - you just need to be careful you’re not messing up your physics engine, or making weird things happen. For example, if your jumper is moving upwards on a fast platform, and you set the velocity as
velocity.y += Math.sqrt(-2 * jumpHeight * gravity);