2d platformer physics

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By darlock

sometimes the player falls, sometimes not.
I want it to be consistent.
how can i fix this?
the video → https://streamable.com/wfl9q
enter image description here
the player will fall if he collides with the left wall
enter image description here

:bust_in_silhouette: Reply From: fershopls

The problem is the way you are adding the gravity.

sp.y += 4

with this code you are adding 4 points of gravity every frame

If your PC runs at 30 fps you will be adding 4 * 30 gravity points
but if your PC runs at 60 fps you will be adding 4 * 60 gravity points
That makes your gravity inconcistent.

You should be adding gravity multiplied by delta.

sp.y += 4 * delta

Note that 4 points it will be to slow since now you are adding 4 points of gravity per second and not per frame

multiplied by delta but the problem is still there.
basically if i collide to that left block then the player will always fall for some reason.
and if i do not collide to it and just go right, then i will not fall.

darlock | 2020-02-29 05:13

I was seeing your code, there is not need to multiply 7 and 9 lines by delta:

sp.x = -3600 * delta

since you are setting it to 3600 and move_and_slide already multiply your linear_velocity (or sp) by delta (its a constant force).

However you need to multiply your gravity addition by delta (u already did it in your code) because you are ADDING a force to your linear_velocity (sp). You need to make your addition constant.

About the main problem I honestly dont know what could be happening

fershopls | 2020-03-01 03:34