0
\$\begingroup\$

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!

\$\endgroup\$
4
  • \$\begingroup\$ Are you assigning to PrevVelocity correctly? \$\endgroup\$
    – user35344
    Sep 4, 2018 at 16:00
  • \$\begingroup\$ Yes, PrevVelocity = CurrentVelocity at the start of the Update loop, before all CurrentVelocity calculations happen. CurrentVelocity is stored in the Player class, so its persistant as well. Checked it in debug mode and it is storing correctly. \$\endgroup\$ Sep 4, 2018 at 18:02
  • \$\begingroup\$ I don't understand why you'd use realistic gravity numbers, unless your game is a real-life simulation, which a 2D sidescroller probably shouldn't. Your characters are pixels, if you need gravity acceleration to be 20 instead of 9.8, then so be it. If your game is more fun with a gravity of 5, then gravity should be 5. I find it silly to emulate real-life when making a video game that is mean to help you escape from it. \$\endgroup\$
    – TomTsagk
    Dec 18, 2018 at 10:11
  • \$\begingroup\$ You're setting PixelsToMeters (which I'm assuming actually means MetersToPixels) to 32 here. A meter is a pretty long distance. A span of 32 pixels comparatively is very short on common display resolutions. Even if you're running at a tiny internal resolution of 640x480, your screen will show 15 meters of vertical distance at all times, which is similar to viewing a scene from very far away. Realistic movement is going to look slow on those scales. \$\endgroup\$
    – Cubic
    Jul 7 at 10:51

1 Answer 1

0
\$\begingroup\$

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 :)

\$\endgroup\$
1
  • \$\begingroup\$ Just tried this and I get the same behavior. The downward acceleration is too slow, resulting in the floating feeling. Essentially what you asked was for me to just remove the halfATSq part from the CurrentDelta, right? That would actually slow down the acceleration, as halfATSq adds just a bit. Im wondering if Im doing the calculations in the wrong place or out of order or what? I had the same kind of issue with Unity once, and the issue was with the scale of the world I had. Scaled it down to the 1 meter size and it worked fine. Going to try multiplying things real quick by PixelsToMeters... \$\endgroup\$ Sep 4, 2018 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .