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

  • Context: Undergrad 
  • Thread starter Thread starter SoBinary
  • Start date Start date
  • Tags Tags
    2d Game Initial
Click For Summary

Discussion Overview

The discussion revolves around calculating the initial vertical velocity required for a character in a 2D game to reach a specified height during a jump. It encompasses theoretical aspects of physics applied to game mechanics, including the effects of gravity and the integration of motion equations.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • Post 1 introduces the problem of determining the initial vertical velocity needed for a character to achieve a certain jump height, outlining the character's movement mechanics and the role of gravity.
  • Post 2 questions the units of the acceleration due to gravity, suggesting a potential mismatch between physical units and the game's pixel-based system.
  • Post 3 emphasizes the need to abstract physical limitations related to frame rate and pixel representation, advocating for a consistent unit system for calculations.
  • Post 4 presents a mathematical framework using ordinary differential equations (ODEs) to describe motion, suggesting that while numerical integration methods can be used, an analytical approach based on energy conservation may provide a simpler estimate for the initial velocity.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate units for gravity and the implications of using pixel-based measurements in physics calculations. There is no consensus on the best approach to integrate the equations of motion or the necessity of unit conversion.

Contextual Notes

Limitations include the potential mismatch of units between physical laws and game mechanics, as well as the unresolved details regarding the integration methods for the motion equations.

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:
Physics news on Phys.org
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:

\frac{dr}{dt} = v \\<br /> \frac{dv}{dt} = a

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 r(t + \Delta t) = r(t) + v(t) \Delta t 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.

\frac{1}{2} m v_y^2 = mgh \implies v_y = \sqrt{2gh}

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
2K
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · 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
4K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 14 ·
Replies
14
Views
2K