Possible correction: Acceleration and multiplying with delta

Towards the end of the video you mentioned that we multiply GRAVITY with delta because we are accelerating:

motion.y += GRAVITY * delta

move_and_slide function multiplies motion by delta once more so in fact it is multiplied with delta squared. This is being justified by formula:

acceleration = speed * time²

First off this equation seems incorrect. Acceleration has units (for example) [m/s²], speed [m/s] and time [s]. If you multiply speed and time squared, you get [m/s * s²] = [m * s]. The correct formula would in my opinion be:

acceleration = speed_gained / time_interval

Leaving those equations behind and getting to more gamey stuff. According to my knowledge delta is used to compensate for different time between function calls. If GRAVITY is multiplied by delta² instead of just delta, this compensation does not work as intended anymore.

And finally if you don’t manually multiply by delta like in the following line of code, you are accelerating anyway because you are adding to the motion, not setting it. Acceleration actually is the speed you gain in a certain time unit. With following code you’re doing just that: in time unit delta you gain GRAVITY * delta speed in y direction. Multiplication is of course, as you taught us, done by the move_and_slide function.

motion.y += GRAVITY

So after all that pseudo scientific talk my proposition is as follows: if you find my arguments correct, I’d suggest removing multiplication with delta from jumping and falling. I’ve tried it myself and works as expected (although I made a bit different jump model). I’ve also removed the delta argument from functions for processing jumping and falling since it’s no longer needed. The last thing necessary is some constant adjustment for gravity and jump speed.

1 Like

You certainly have a point here…
However - Trying to define the constants and variables as physical entities:

GRAVITY = a (acceleration)
motion = v (speed, or velocity)
displacement is not used in the code, but it is usually called s in physics.

v = a * t
s = v * t
s = a * t2

Delta, I believe, is defined as the time taken since the last frame. This depends on the performance of the computer and the complexity of the game, etc, and is used to make sure that an object moves the same distance per time unit, no matter how long time there is between two frames.

Now, as I (being a newcomer to Godot) understand it is that the move_and_slide function handles motion with regard taken to the delta value.
However - in order to calculate the acceleration due to gravity properly, we actually do need to multiply with delta when calculating it. Otherwise, the gravity would be different depending on the time between two frames - on fast computers it would be huge, and on slow ones you would be jumping on the moon. :slight_smile:

Buuuuut… I might have misunderstood. :wink:

I agree with your understanding of delta and I think you might have a point. Gravity will have impact on motion.y, which we pass to move_and_slide and it gets multiplied there. But my method does not account for how much of this gravity is added to the motion each time. Multiplying with delta would actually make sure the acceleration becomes framerate-dependent and the same on any machine. By multiplying acceleration you get the increase of speed, you modify the motion with it, and motion gets multiplied by delta again because movement of the character should be framerate-dependent too.

The following from Godot documentation made me realize we’re actually dealing with velocity:

move_and_slide() automatically calculates frame-based movement using delta . Do not multiply your velocity vector by delta before passing it to move_and_slide()

I would also comment this formula:

s = a * t²

As far as I understand it t or time here is not delta but rather time from beginning of the movement (which flows from 0 seconds onwards). Why? Suppose you have a constant acceleration a (it actually has to be a constant or you’d have to use integrals) and your framerate (and thus delta) is constant. The result s would always be constant in this case, which is not what you want - you want to move or in other words increase your displacement. The point I wanted to make is that you don’t have information about the time I described earlier and therefore you can’t use this formula in the game. This is why I wrote mine in “derivative” form (a = dv/dt). If I write if differently it becomes dv = a * dt or in our words motion.y += GRAVITY * delta. It actually does justify multiplication of gravity with delta. Just not with the formula that was shown in the lecture. Does this sound about right or did I mess it up?

1 Like

:smiley: Math is so interesting!

I would say that “t” in the formula can mean any kind of time. Time since start (if a is constant) or it can also mean a “delta time”.
And the “delta time” is what you have in your derivative function, right?
You wrote: a = dv/dt
So… in order to find the change in speed (dv) we move (dt) to the other side:
Thus: dv = a * dt
And that is what we have in the game code :wink:
motion.y += GRAVITY * delta
The change (d) in motion (v) is GRAVITY (a) times the change in time, “delta” (dt)

Yes, this is what I meant and I’d agree with it. So the solution shown was right after all (just not explained correctly).

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms