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

  • Context: Undergrad 
  • Thread starter Thread starter SoBinary
  • Start date Start date
  • Tags Tags
    2d Game Initial
SoBinary
Messages
1
Reaction score
0
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:
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.
 
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
 
Once that is ironed out, this is a pretty simple system of ODEs:

[tex]\frac{dr}{dt} = v \\<br /> \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.
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
23K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 14 ·
Replies
14
Views
3K