Why do I need to multiply two times for delta for movement?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gabriel
:warning: Old Version Published before Godot 3 was released.

Hi. On the second code block of this section of the docs (http://docs.godotengine.org/en/stable/tutorials/2d/kinematic_character_2d.html#moving-the-kinematic-character) one can see:

extends KinematicBody2D

const GRAVITY = 200.0
var velocity = Vector2()

func _fixed_process(delta):

    velocity.y += delta * GRAVITY

    var motion = velocity * delta
    move( motion )

func _ready():
    set_fixed_process(true)

In the _fixed_process we multiply a constant by delta:

velocity.y += delta * GRAVITY

I can understand that if we don’t do this step our game would be framerate dependent (at 60 fps our character would move faster than if it were 30 fps), so by multiplying by delta our code becames independent of the framerate.
But I cannot understand this step:

var motion = velocity * delta

Why multiply again?

Thanks in advance.

:bust_in_silhouette: Reply From: ericdl

It is only being multiplied by delta once.

In this line:

velocity.y += delta * GRAVITY

delta is multiplied by GRAVITY, and then added tovelocity.y every physics frame.

:bust_in_silhouette: Reply From: Joubarbe

You missed the “+=” sign.

velocity.y += delta * GRAVITY

is the same as in:

velocity.y = velocity.y + delta * GRAVITY
:bust_in_silhouette: Reply From: Zylann

It appears to be multiplied twice because of the measurement units in use.

motion is in pixels. velocity is in pixels per second (a speed), and GRAVITY is in pixels per second per second (an acceleration of the speed).

When you add GRAVITY to velocity, it needs to be the same measurement unit, so we multiply it by the time elapsed, delta. This makes the speed increase over time:
(pixels/seconds) + ((pixels/seconds)/seconds) * (seconds) = (pixels/seconds)

Then again, when you add velocity to motion, it needs to be in pixels, so we multiply it by the time elapsed, which makes the position change over time.
(pixels) + (pixels/seconds) * (seconds) = (pixels)

Using delta on velocity is almost always required in games so speed is framerate independant. It’s also the case for GRAVITY, because otherwise acceleration would be framerate dependent. For example, you would reach maximum fall speed faster at high FPS than in low FPS.

Zylann, perfect explanation!

jospic | 2016-11-25 16:47

Are those supposed to be assignments or equalities? Because dividing by seconds then multiplying by seconds will cancel out, leaving with pixels + pixels = pixels.

avencherus | 2016-11-25 17:52

They are equalities, kinda. If you replace variables by their units of measure, that’s what you would get. I tried to show how units should keep consistent, this is very useful when you want to ensure that a formula makes sense :slight_smile: (adding pixels to pixels gives you pixels)

Zylann | 2016-11-25 18:21

Great. Thanks.

Gabriel | 2016-11-26 00:15

:bust_in_silhouette: Reply From: eons

My english is not good enough for this but I’ll try…
In addition to the other answers:

Is because you may want to emulate the Uniformly Accelerated Motion.

r = r0 + v0*t + 1/2*a*t²

Then, the ammount to move

r-r0 = ( v0 + 1/2*a*t) * t

That, with integration on top (using delta/differential times) you will get a good aproximation of real motion in a time lapse.

On Wikipedia:

https://en.wikipedia.org/wiki/Equations_of_motion#Uniform_acceleration

Physics hypertextbook:

http://physics.info/motion-equations/

But your game has it own laws, it does not have to follow real physics laws, you can sacrifice stuff for the sake of player’s gaming experience.