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:
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 (adding pixels to pixels gives you pixels)