2D Sidescroller Gravity

Game Development Asked by Pete Mavrelis on October 22, 2020

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!

One Answer

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

Add your own answers!

Related Questions

Convert a string to a gameobject name

1  Asked on December 1, 2021 by stormzy18


Jumping up quicker? (Paper Mario style jump)

1  Asked on December 1, 2021 by spottel


A* Pathfinding Finding One of Many Goals

1  Asked on November 29, 2021 by user6256


Wheel rotation for a raycast car in Unity?

1  Asked on November 29, 2021 by john-545


Unity “scrape away” effect

0  Asked on November 24, 2021 by darren-swords


Initial setting of camera and ppu to match playing table

1  Asked on November 21, 2021 by zibelas


LibGDX – Tilemap Wall Collision Detection Issues

2  Asked on November 20, 2021 by james-stoddern


Tilemaps not showing up on Android build

1  Asked on November 18, 2021 by jcorretjer


Unity: Sprite stretch issue

1  Asked on November 14, 2021 by tikeb


Unity UI, how to hide on play

2  Asked on November 12, 2021


Alternative path not working

1  Asked on November 12, 2021 by dsngl-tibor


Unity – Understanding the SRP batcher

2  Asked on November 12, 2021 by majs


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir