Ive looked up implementing gravity on several sites. Subtract a constant gravity (9.8 * DeltaTime) from the velocity each frame. My problem is, when implementing it as they say, when walking off a ledge the player will float off, not vertically, just accelerates too slowly as he falls.
World->Gravity = -9.8 * PixelsToMeters; // PixelsToMeters = 32 CurrentAcceleration = Vector2(0.0f, World->Gravity); CurrentVelocity = (CurrentAcceleration * DeltaTime) + PrevVelocity; Vector2 halfATSq = currentAcceleration * 0.5f * Square(DeltaTime); Vector2 vt = CurrentVelocity * DeltaTime; CurrentDelta = halfATSq + vt; CurrentPosition += CurrentDelta;
Now the thing is, with the 1/2at^2 + vt + p calculation, the character does fall about 20 meters in 2 seconds, (I think those are the numbers I used, that was a couple hours ago), so it seems the calculations are right. Ive played quite a few side scrollers and none of them have this floating feel.
So, I tried calculating the gravity based on a TimeInAir variable that multiplies the World->Gravity on the third line, instead of DeltaTime.
CurrentVelocity = Vector2(currentAcceleration.X * DeltaTime, currentAcceleration.Y * TimeInAir) + PrevVelocity;
He no longer floats, he falls as expected. Even jumping looks better. However, now he falls 20 meters in 1 second. Ill deal with this, it is only a game and not reality.
Question is, why is it that this works instead of the original code? Or do I have an error somewhere? Because I have to rework the physics code to get it to look right for everything, instead of just using the basic equations. Is it because its all calculated in pixels? Im wondering if I need to add a full gravity to the velocity on the first frame instead of gravity * DeltaTime? This works for falling off a ledge, but not for jumping.
Im testing these questions as I think of them in my code, and still not getting it. These ideas dont seem to work. Thanks for looking and whatever help or ideas you can give!
Subtract a constant gravity (9.8 * DeltaTime)
Just FYI, 9.81 m/s^2 is not "Gravity". It's acceleration due to Gravity (on Earth).
Also, it looks like you are mixing up the kinematic equations. The equation you are using is:
0.5*a*t*t + v*t + p
This equation calculates the position based on initial velocity and initial position with constant acceleration, so you shouldn't be recalculating your velocity, and then feeding it back into the equation to calculate the distance. The equation already accounts for change in velocity with the 1/2at^2 term. Instead use prevVelocity here, and recalculate currentVelocity for use in the next frame.
I would recommend just using:
v += a * deltaT p += v * deltaT
Also, sidescroller games don't always use 9.81m/s^2 as their acceleration due to gravity. Maybe this is just because it isn't snappy enough. If your game takes place in a different world, then you can use whatever acceleration you want and still be correct :)
Answered by Kyy13 on October 22, 2020
0 Asked on December 3, 2021
1 Asked on December 1, 2021 by spottel
1 Asked on November 29, 2021 by john-545
1 Asked on November 29, 2021 by trojen
1 Asked on November 29, 2021
1 Asked on November 26, 2021
1 Asked on November 21, 2021
1 Asked on November 21, 2021 by zibelas
2 Asked on November 20, 2021 by james-stoddern
2 Asked on November 20, 2021
1 Asked on November 20, 2021
1 Asked on November 20, 2021
Get help from others!