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

In summary, the character needs to jump to a certain height, so they need to calculate their initial vertical velocity. They need to use the laws of physics, which are expressed in meters, seconds, etc., to calculate the height the character will get.
  • #1
SoBinary
1
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
  • #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.
 
  • #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
 
  • #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.
 
  • #5


To calculate the initial vertical velocity required for the character to reach a certain height, we can use the kinematic equation: vf^2 = vi^2 + 2ad, where vf is the final velocity, vi is the initial velocity, a is the acceleration (in this case, gravity), and d is the vertical distance traveled.

In this scenario, we know the final velocity will be zero at the peak of the jump, and the vertical distance traveled will be the desired height. Therefore, we can rearrange the equation to solve for the initial velocity:

vi = √(2ad)

Plugging in the values of gravity and the desired height, we can calculate the initial vertical velocity needed for the character to reach that height. It is important to note that this calculation assumes a perfectly vertical jump and does not take into account any external factors such as air resistance.

Additionally, it may be helpful to consider the character's horizontal movement and adjust the initial velocity accordingly in order to achieve the desired trajectory. This can be done by breaking down the initial velocity into its horizontal and vertical components and applying them accordingly.

Overall, by using the kinematic equation and understanding the character's movement and gravity in the game, we can accurately calculate the initial vertical velocity needed for the character to make a successful jump to a specific height.
 

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

1. What is initial Y-velocity in a 2D game?

Initial Y-velocity in a 2D game is the starting velocity of an object in the vertical direction. In other words, it is the speed at which an object is moving up or down when it is first launched or dropped in a 2D game.

2. Why is initial Y-velocity important in a 2D game?

Initial Y-velocity is important in a 2D game because it determines the trajectory of an object's motion in the vertical direction. It affects how high or low an object will go, as well as how quickly it will reach its peak height. It also plays a role in collisions and interactions between objects in the game.

3. How can I calculate initial Y-velocity in a 2D game?

To calculate initial Y-velocity in a 2D game, you will need to know the initial height of the object, the time it takes for the object to reach its peak height, and the acceleration due to gravity. You can use the formula V = V0 + at, where V is the final velocity, V0 is the initial velocity, a is acceleration, and t is time. Rearranging this formula, you can solve for V0 and find the initial Y-velocity.

4. How does initial Y-velocity affect gameplay in a 2D game?

Initial Y-velocity can greatly affect gameplay in a 2D game. A higher initial Y-velocity can make an object reach higher heights and move more quickly, while a lower initial Y-velocity will result in lower jumps and slower movement. This can impact the difficulty of the game, as well as the strategies and techniques players may use.

5. Can initial Y-velocity change during gameplay in a 2D game?

Yes, initial Y-velocity can change during gameplay in a 2D game. This can happen if the object is affected by external forces, such as wind or other objects, or if the player inputs a different velocity. Changes in initial Y-velocity can also occur when the game environment changes, such as when an object moves from one level to another or enters a different area with different gravity or obstacles.

Similar threads

  • Introductory Physics Homework Help
Replies
11
Views
299
Replies
36
Views
20K
Replies
17
Views
2K
Replies
14
Views
1K
  • Advanced Physics Homework Help
Replies
1
Views
941
  • Introductory Physics Homework Help
Replies
7
Views
2K
Replies
12
Views
2K
  • Introductory Physics Homework Help
Replies
5
Views
2K
Back
Top