I'm making a game engine and it's time to make realistic jumps. My problem is I can't figure out what jump velocity to set for the player.

There is a gravity vector that decreases player's velocity by G pixels every frame, and the initial jump vector J that only adds to the total momentum once, when the player jumps. I want to be able to simply set a number of pixels for how high the character will jump. How do I make that formula?

  • $\begingroup$ If you know appropriate tags, please add them to the question. $\endgroup$ – user1306322 Oct 28 '12 at 8:56
  • $\begingroup$ "a gravity vector that decreases player's position by G pixels every frame" -- Shouldn't it be the player's velocity you're decreasing? $\endgroup$ – Rahul Oct 28 '12 at 9:23
  • $\begingroup$ Yeah, I meant that. Thanks. $\endgroup$ – user1306322 Oct 28 '12 at 9:25

If at each frame you do $y=y+v_y$ and $v_y = v_y-g$ and start with $v_y=v_0$, then the top will be reached after $\frac{v_0}g$ frames and a height difference of $\frac{v_0^2}{2g}$. Thus to jump a height $h$, you need $v_0=\sqrt {2 g h}$. Some minor adjustment may be necessary dpending on your needs if $v_0$ is not a multiple of $g$, i.e. if the top is reached between two frames.

  • $\begingroup$ Somehow the correct value is found using Sqrt(gh/2). I decrease velocity.Y by G each frame after adding H to it once. $\endgroup$ – user1306322 Oct 28 '12 at 9:39
  • $\begingroup$ I found the problem: my update method was incorrect, now it works with your formula as expected. $\endgroup$ – user1306322 Oct 28 '12 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.