How is it that the amount of pixels a player moves is constant when multiplying it by delta time?

I know that when you don't use delta time, the players speed will be affected by how many FPS your pc runs the game. Meaning if i can run the game at 60fps, my player would move many more pixels than if my PC were running the game at 30fps. But how is that delta time fixes this and makes it so the players always move the same amount of pixels regardless of my frame rate?

I know that the delta time calculates the time it takes to process one frame to the next. But how does this make it so the pixels is constant? Depending on my machine, it may take more time to go frame one frame to the next. So how does it make the pixel rate constant? I'm so confused

Posted Feb 8, 2021, 4:54 AM UTC

36
zaidazadkiel•

You have to decide on the pixels per second your objects move Thats the speed you multiply by delta -> 0.16 for 60fps So each frame your obj moves 0.16 of the distance it will move in a second

That way if your frame takes longer, yiur obj will skip frames to the real time position instead of slowing down

Likewise if you draw in less than .16sec your obj will draw more times to move the exact same distance

4
someguyhere0OP•

I think I get it.

So basically, if I want to move my player 15px with every frame, that would mean the player would move 900px per second at 60FPS. However if my machine was not as fast and it ran the game at 30FPS, it will still achieve a 900px per second movement by skipping frames? Meaning it would draw the player less times on the screen so it can still move the same distance?

3
zaidazadkiel•

Yepperz you got it

1
someguyhere0OP•

Thank you!

2
GroovyDXZ•

I like to think of it in terms of unit.

What you typically do in a game, is to define a "standard speed unit" : how many meters (or pixels or whichever unit you want to use) the object should travel per second I.e. "meters/seconds".

The thing is, the physical step calculations happens once every frame, so when you write e.g. myObject.position.x += 5; you are actually defining its speed per frame to be, in this example, 5 "meters/frame".

So what you need to do, is to convert that standard "meters/second" speed to a "meters/frame" speed. And to do that, you need to get ride of the "second" part by multiplying by a "second/frame" unit (when multiplying, the "second" will cancel each other leaving you with only meters/frame). As you may have guessed, "second/frame" is exactly what deltaTime is.

In conclusion, writing myObject.position.x += 5 * Time.deltaTime; is like saying "alright, the speed I want is 5 m/s but since this code is executed every frame, I will convert it to m/frame by multiplying with deltaTime".

Hope this is useful, as I have also struggled to understand this concept.

2
[deleted]•

[deleted]

1
Nanaki404•

A speed is a distance divided by a time (for example 150 pixels / second).

If you want to know the distance moved during some time T, you just multiply the speed by T.

If you want to know the distance moved during a frame, you just multiply the frame duration (i.e. the delta time) by the speed.

1