This snippet uses impulses to move Box2D bodies.

One problem that arrises with platformers is jumping. Specifically, how you do get a player to jump a certain height? Within Moai using Box2D, this can be achieved with a trivial calculation.

In classical mechanics, the equation *v1^2 = v0^2 + 2*a*d* shows that the square of the final velocity is equal to the square of the initial velocity plus the acceleration times the distance times two. What a mouthfull.

We know at the peak of the jump the velocity will be 0 at the distance of the height of the jump. Also, the acceleration is simply the value of gravity (-9.8 for example). With this information, just simplify the equation...

*0 = v0^2 + 2*a*d*
*v0^2 = -2*a*d*
*v0 = sqrt(-2*a*d)*

Now you have a target velocity to use with an impulse to reach your wanted height.

Quick note: If you want to be able to clear a maximum certain height, make sure your jump height is slightly higher, otherwise, your body will never make it over.

## Be the first to comment