## What's the right way to calculate time? Counting by time or counting by frame?

The Partridge Family were neither partridges nor a family. Discuss.
WilsonHuang
Posts: 33
Joined: February 13th, 2019, 3:23 am

### What's the right way to calculate time? Counting by time or counting by frame?

Hi, guys, I found out there's two ways to calculate time. One is counting by time another counting by frame.

Let's say we need to move a character in a period of time. One way we could count by time
Like this:

Code: Select all

``````dt = ft.GetFrameTime();
gameTime += dt;
if (gameTime >= 1.0f)
{
gameTime -= 1.0f;
character.Move();
}
``````
Or we can count by frame
Like this:

Code: Select all

``````frameCounter += 1;
if (frameCounter >= 60) //we should get user frame rate here
{
frameCounter -= 60;
character.Move();
}
``````
Which way is correct or people usually do? Or it depends on situations
If we count by time how do we deal with frame skip issue?
Thanks.

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

### Re: What's the right way to calculate time? Counting by time or counting by frame?

Considering you are wanting time elapsed since last check, you should use time. Time is constant whereas frame rate is not.

For instance, let's say you have a 140Hz monitor. Your frame rate is capped at 140 frames per second if v-sync is enabled whereas my monitor is 60Hz, so I'm capped at 60 frames per second with v-sync enabled. Since it's frames per second and you assume the game runs at 60 frames per second what happens on my computer may take 1 second whereas your computer with the 140 frames per second will do in about .4 seconds.

The other way is true also. If someone else's computer can't even do 60 fps and does only 30 fps, it's going to take them 2 seconds to do the same action.

Long story short, always calculate your delta using time since it's constant regardless of frame rate or processing power of the machine.
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

WilsonHuang
Posts: 33
Joined: February 13th, 2019, 3:23 am

### Re: What's the right way to calculate time? Counting by time or counting by frame?

albinopapa wrote:
May 11th, 2020, 12:42 am
Considering you are wanting time elapsed since last check, you should use time. Time is constant whereas frame rate is not.

For instance, let's say you have a 140Hz monitor. Your frame rate is capped at 140 frames per second if v-sync is enabled whereas my monitor is 60Hz, so I'm capped at 60 frames per second with v-sync enabled. Since it's frames per second and you assume the game runs at 60 frames per second what happens on my computer may take 1 second whereas your computer with the 140 frames per second will do in about .4 seconds.

The other way is true also. If someone else's computer can't even do 60 fps and does only 30 fps, it's going to take them 2 seconds to do the same action.

Long story short, always calculate your delta using time since it's constant regardless of frame rate or processing power of the machine.
Understood, Thanks for explanation.