Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Initial Y-Velocity (time unknown) in 2D Game

  1. Jul 16, 2012 #1
    I have a character who needs to jump to a certain height, I want to calculate his initial vertical velocity so that he can achieve that height. Distances and speeds are in logical pixels per second.

    The character's position is stored in a 2D vector \begin{pmatrix} x \\ y\end{pmatrix}, and movement is enacted by adding a velocity vector \begin{pmatrix} vx \\ vy\end{pmatrix} to the position vector at every frame. Gravity, with value \begin{pmatrix} 0 \\ -9.8\end{pmatrix} is added to the velocity vector if the character is not in the air.

    At frame n, the vy_n can be expressed as vy_n = vy_(n-1) + Gravity, which gives position at frame n y_n = y_{(n-1)} + (vy_n * Δ) where Δ is delta time (the seconds in between two frames, say 1/60)

    So, in order to get the character to height h, what should the initial vy be?
    Last edited: Jul 16, 2012
  2. jcsd
  3. Jul 16, 2012 #2
    Big problem right off the bat: what are the units of your acceleration due to gravity? That looks like meters per second per second, not pixels per frame per frame, unless you want to continually convert between unit systems.
  4. Jul 16, 2012 #3
    Muphrid is right,
    you have to abstract away your current physcical limitations (framerate, pixels)
    The laws of physics you are trying to exploit are expressed in meters, seconds, etc.
    You must have a translation somewhere to translate how many meters your characters advances in order to draw it in you pixels based basis.
    the same goes for time, which you really don't want to tie to the refresh rate of the monitor, unless you are programming for a very old device where those 'tricks' were not only acceptable but quite necessary and in many cases strokes of genius
  5. Jul 16, 2012 #4
    Once that is ironed out, this is a pretty simple system of ODEs:

    [tex]\frac{dr}{dt} = v \\
    \frac{dv}{dt} = a[/tex]

    And in principle, you know or can calculate the velocity and acceleration at any given timestep. How you choose to integrate these ODEs is up to you--a very simple Newton's method would be to say [itex]r(t + \Delta t) = r(t) + v(t) \Delta t[/itex] and be done with it, but there are more sophisticated methods if you have the inclination to try them.

    The exact method you choose to integrate these equations numerically is a bit beyond the point, though. You just need a ballpark estimate of how high the character will get. That's probably best done analytically and can be done from an energy standpoint.

    [tex]\frac{1}{2} m v_y^2 = mgh \implies v_y = \sqrt{2gh}[/tex]

    The numerical method you use will likely not give exactly the correct height, but as long as you have someone check the error of the method and ensure it's small for the time between steps that you choose, it should be fine.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Initial Y-Velocity (time unknown) in 2D Game