Hello !
I'm creating a game as a project for my Bachelor's Degree using the SDL lib and a simple implementation of an ECS I'm also making. The game will be a simple platformer-shooter 2D. I was trying to make my character jump, but he doesn't want to fall to the ground, as long as I keep the jump key pressed, he fly away, and only start falling after I release the key. I don't really understand what I'm doing wrong, I created a boolean "jumping" which change to true whenever the key is pressed, and turn back to false when the character touch the ground, but that didn't solve the problem... Here are my physics function, my rigidbody class, and my key handling for the jumping character :
it looks like you have a state, possibly line 72 in the last chunk, that says you are jumping or not.
you need instead a timer, such that a jump is an event that launches you with an initial velocity at an angle and off you go. Then every time slice, you do the physics with gravity and all, via components and such. It looks like you have most of that in place, except the concept of TIME. Time is very important to this kind of work, and pardon me if its in there and I am just missing it.
consider the highschool based physics formula ** ...
dist = 0.5(gravity*time*time) + initial velocity
**they make you memorize this early on. Later you see that its the derivative and the calc stuff. But when doing this on a computer, you need it in a form you can code, not textbook calc formulas.
Under constant acceleration:
v_{f} = v_{0} + a * Δt
s_{f} = s_{0} + v_{0} *Δt + (1/2) * a * Δt^{2}
s_{0} is initial position
v_{0} is initial velocity
a is the acceleration. a = f/m (force divided by mass). For gravity, the force is proportional to mass, so it's g, the acceleration due to gravity.
Δt is the amount of time elapsed.
s_{f} is final position - the position after Δt elapses
v_{f} is the final velocity - the velocity after Δt elapses.
Be sure to pick your coordinates and stick with them. For example, if up is positive, then acceleration from gravity is -9.8m/s^{2}. It's negative because it pulls in the negative direction (down).
when you jump, get the time.
until you land, apply falling physics to the Y component, using 'now' since start time for current position.
you need a way to know if you are on the ground, so you can allow jumping again (else attempt to jump ignored).
units matter too :) now do be in integers where 1 == 1ms not 1s
that aside, the missed units should just change the rate of fall, so if it isnt falling, something else is wrong. I don't see the 1/2 part either.
mass isnt important here. 1/2att is the core of it, with adjustments for initial position and initial velocity.
gravity is a force. but you are computing a change in distance.
look. if you start at the top of a 500m tall building at time = 0 and drop a ball, where is it after 1.23 seconds?
d = 4.9 * 1.23 * 1.23 = 7.41... m .. you move the object that much in pixels towards the ground. How many pixels to a meter?
how long does it take to fall, then? sqrt(500/4.9) = 10.10... sec
do you follow that much? The rest is just minor adjustments until you get into aerodynamics and earth rotation and all that mess for advanced ballistics.
there should not be any forces(other than gravity) or masses in your positional calculation. If there is a force on the thing, you need to turn that into a distance as well, eg if you throw a lit bottle rocket up off the top of a building.
Well it does fall, but only when I release the jump key, so I assume that the problem come from my event handler, but I don't see where can it be :p
Although, I think I didn't understand the calculation at all...
did you have high school or college physics at all :)
the first thing they drill into you is that units ... you can't add different units.
your units version of the equation you gave is
speed = force + distance ?!
no can do.
distance = distance + distance is ok.
that is what we were computing, see my example first equation, I get 7 meters. That is what happened. In the time given, the thing fell 7 meters.
so you need
distance = distance. a jump applies a force, an acceleration to a mass, over a time, and moves it some amount. if you jump straight up, gravity pulls you back down, so you go up a little then fall back.
speed is a distance in an amount of time, 5 miles in an hour, like in a car.
so a speed times the amount of time is a distance, like an initial velocity * time. This is show above in the equations Dhayden gave : v0 *Δt
do you have or can you get the initial velocity from what you have available easily?
v = u + at
s = ut + (1/2)a(t^2)
(v^2) = (u^2) + 2as
v = final velocity
u = initial velocity
s = distance moved
a = acceleration (g for gravity)
t = time
If t is seconds
n, v is metres/second
a is metres/second/second
s is metres
With gravity it's important to decide which direction is negative. If upwards is positive then a will be (-g) in the above.
In Physics there is the concept of dimensions. The fundamental ones involved here being mass(M), length(L), time(T). Each element of a formula (such as v = u + at) is expressed in its fundamental dimensions. Then both sides of the formula should match.
So for v = u + at, v is L/T, u is L/T, a is L / (T^2), t is T. So the RHS is
L/T + LT/(T^2) which is L/T + L/T which is of dimension L/T. The LHS is L/T. Both sides are of the same dimensions so the formula is dimensionally correct.