## Beginner T17 <chrono> FPS explanation

The Partridge Family were neither partridges nor a family. Discuss.
Jankko
Posts: 5
Joined: September 2nd, 2020, 9:58 am

### Beginner T17 <chrono> FPS explanation

Hello fellow programmers,

I'm seeking help with understanding of how exactly does the transfer to FPS logic work in tutorial 17.
I understand, that the function measures the time between each frame which is e.g. 0.03 sec but how does it make exactly 1 sec. from that (so that the sprites move by given amount of pixels each second)?

Might be dumb question but I'm probably missing some logic behind that and I would like to understand it before I move on.
If someone could explain it to me like I was a chimpanzee, that would be great.

albinopapa
Posts: 4294
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Beginner T17 <chrono> FPS explanation

Let's say you you want something to move 100 pixels/sec. If you get 60 frames per second, you'd want to divide 100/60 to get pixels per frame. Since time per frame can vary, you'd want to get the time since last frame and multiply that by the "speed", so in your example of .03 seconds, you get 3 pixels for that frame.

It probably won't come out to 1 second exactly over the frames, but as far as movement goes, it should be close enough.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Jankko
Posts: 5
Joined: September 2nd, 2020, 9:58 am

### Re: Beginner T17 <chrono> FPS explanation

So basically it doesn't get influenced by the refresh rate of the screen because even if you had 120fps screen the time between the cycles would get to 0.015 so it would move by 1.5px but 120 times per second so it will end up the same as 60fps with 0.03 between the cycles, is that right?

tl;dr 120 * 1,5 = 60 * 3

albinopapa
Posts: 4294
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Beginner T17 <chrono> FPS explanation

Your understanding is correct, but the math is off:
120 fps = 8 milliseconds per frame
60 fps = 16 milliseconds per frame
30 fps = 33 milliseconds per frame

However, refresh rate only caps the framerate if vsync is on. Let's now say that you have so much going on and even though your refresh rate is 60Hz, your game is only getting 40 frames per second.
40 / 1000 = .025 milliseconds per frame. Your character moves 2.5 pixels ( 2 because of integer conversion ). So still over 1 second or 1000 milliseconds, you move around the 100 pixels per second regardless of framerate.

Physics engines like to keep a constant interval because it "makes the math easier". Your framerate can vary, but the rate at which collisions are checked and movements are done are all based on say .02 or 50 times a second, but the frame rate can be anywhere between 1 and whatever. I play Rocket League, they claim there are 120 physics updates per second so every 8 milliseconds, but my graphics frame rate on my low end computer only gets up to ~80 fps or 12.5 milliseconds per frame.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Jankko
Posts: 5
Joined: September 2nd, 2020, 9:58 am

### Re: Beginner T17 <chrono> FPS explanation

I think i got it, you know I feel really dumb now, what i didn't understand is that the sprite moves by the time.
Basically the more time the cycle takes, the more the sprite moves right, e.g. the cycle would for some reason take 3 seconds and the speed of the sprite was set to 3px it would move by 9px during 1 cycle because it took such a long time. I was looking for something more complicated than this logic, my bad. Thanks a lot for deep explanation.

albinopapa
Posts: 4294
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

### Re: Beginner T17 <chrono> FPS explanation

Yep, you got it.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com