#### Jordan Robinson

##### Member

Jumping to a Specific Height

Jordan Robinson

Jordan Robinson

**GM Version:**1.4.1757**Target Platform:**ALL**Download:**N/A**Links:**N/A**Summary**:

This tutorial will explain how you can easily make your player jump to a specific height, thus giving you more control over movement.

**Tutorial:**

The end result of this tutorial will be a small formula, easily implemented with a bit of GML, which will have our player jumping exactly how we want. But before we get to the coding, we need to look at a little bit of theory.

A couple of terms I will be using here are kinetic energy and potential energy. Before the player jumps, his

**potential energy**(Pe) is 0 (with regard to his vertical motion). At the top of the jump, the player's

**kinetic energy**(Ke) is 0. Therefore, we can say that: Ke(top) = Pe(bottom)

Ke = 0.5mv^2

where m = mass and v = velocity.

Pe = mgh

where m = mass, g = gravity acceleration, and h = height.

We now have everything we need to start working out our formula.

Ke(top) = Pe(bottom)

0.5mv^2 = mgh

Simplifying the equation:

0.5v^2 = gh

v^2 = 2gh

v = √2gh

Result:

v = √2gh

where v is the jump speed required to reach the specified height h, given the acceleration due to gravity g.

So the final part is to implement this formula in GML. There are a couple of important points to remember when doing this:

- The value of y increases as you move down the screen. Therefore, our jump speed has to be negative.
- Acceleration due to gravity should be the value you are adding to your player's vspeed each step.
- Height will be the height in pixels you want the player to jump from his current position.
- This equation is dependent on the value for gravity, so the room speed will not affect it. The player will always reach your desired height provided you use the correct values.

Code:

```
if(keyboard_check_pressed(vk_space))
{
vspeed = -sqrt(2 * gravity * height); //Replace height with your desired height.
}
```

Last edited: