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

  • Thread starter Thread starter SoBinary
  • Start date Start date
  • Tags Tags
    2d Game Initial
AI Thread Summary
To calculate the initial vertical velocity needed for a character to jump to a specific height in a 2D game, one must consider the effects of gravity and the units used for acceleration. The character's movement is governed by a velocity vector that updates each frame, incorporating gravity when the character is grounded. It’s crucial to establish a consistent unit system, as mixing pixels with physical measurements can complicate calculations. The relationship between velocity, acceleration, and height can be simplified using energy principles, leading to the formula v_y = √(2gh) for initial velocity. Ultimately, while numerical integration methods can approximate the character's trajectory, ensuring minimal error in calculations is essential for accurate results.
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.
 
The rope is tied into the person (the load of 200 pounds) and the rope goes up from the person to a fixed pulley and back down to his hands. He hauls the rope to suspend himself in the air. What is the mechanical advantage of the system? The person will indeed only have to lift half of his body weight (roughly 100 pounds) because he now lessened the load by that same amount. This APPEARS to be a 2:1 because he can hold himself with half the force, but my question is: is that mechanical...
Hello everyone, Consider the problem in which a car is told to travel at 30 km/h for L kilometers and then at 60 km/h for another L kilometers. Next, you are asked to determine the average speed. My question is: although we know that the average speed in this case is the harmonic mean of the two speeds, is it also possible to state that the average speed over this 2L-kilometer stretch can be obtained as a weighted average of the two speeds? Best regards, DaTario
Some physics textbook writer told me that Newton's first law applies only on bodies that feel no interactions at all. He said that if a body is on rest or moves in constant velocity, there is no external force acting on it. But I have heard another form of the law that says the net force acting on a body must be zero. This means there is interactions involved after all. So which one is correct?
Back
Top