# DeltaTime is smaller than it should be

I have a gameloop for a simulation I'm making, I noticed that the simulation is going very slow, When i checked the DeltaTime it was very small, I set the target fps to be 1 (deltatime should be 1 in that case) and it was still very small (1.1920929E-07) which is making the simulation super slow even with huge numbers.

Here's the gameloop code

    public void Run()
{
Ticks = 0;
_running = true;

float totalTimeBeforeUpdate = 0;
float previousTimeElapsed = 0;

var clock = new Stopwatch();
clock.Start();

while (_running)
{
var totalTimeElapsed = (float) clock.Elapsed.TotalSeconds;
var deltaTime = totalTimeElapsed - previousTimeElapsed;
previousTimeElapsed = totalTimeElapsed;

totalTimeBeforeUpdate += deltaTime;

if (totalTimeBeforeUpdate >= TimeUntilUpdate)
{
GameTime.Update(deltaTime, (float) clock.Elapsed.TotalSeconds);
totalTimeBeforeUpdate = 0;

Update(GameTime);

Draw(GameTime);

Ticks++;
}
}
}


Edit: Yes I'm getting the correct target fps

Edit: Here's the results I'm getting when debugging

Ticks: 0, Total Time Elapsed: 1.0002049, Delta Time: 1E-07, FPS: 10000000
Ticks: 1, Total Time Elapsed: 2.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 2, Total Time Elapsed: 3.0000003, Delta Time: 1E-07, FPS: 10000000
Ticks: 3, Total Time Elapsed: 4.0000004, Delta Time: 1E-07, FPS: 10000000
Ticks: 4, Total Time Elapsed: 5.0000004, Delta Time: 1E-07, FPS: 10000000
Ticks: 5, Total Time Elapsed: 6.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 6, Total Time Elapsed: 7.0000004, Delta Time: 1E-07, FPS: 10000000
Ticks: 7, Total Time Elapsed: 8.0000003, Delta Time: 1E-07, FPS: 10000000
Ticks: 8, Total Time Elapsed: 9.0000002, Delta Time: 1E-07, FPS: 10000000
Ticks: 9, Total Time Elapsed: 10.0000003, Delta Time: 1E-07, FPS: 10000000
Ticks: 10, Total Time Elapsed: 11.0000012, Delta Time: 1E-07, FPS: 10000000
Ticks: 11, Total Time Elapsed: 12.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 12, Total Time Elapsed: 13.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 13, Total Time Elapsed: 14.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 14, Total Time Elapsed: 15.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 15, Total Time Elapsed: 16.0000004, Delta Time: 1E-07, FPS: 10000000
Ticks: 16, Total Time Elapsed: 17.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 17, Total Time Elapsed: 18.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 18, Total Time Elapsed: 19.0000006, Delta Time: 1E-07, FPS: 10000000
Ticks: 19, Total Time Elapsed: 20.0000005, Delta Time: 1E-07, FPS: 10000000
Ticks: 20, Total Time Elapsed: 21.0000006, Delta Time: 1E-07, FPS: 10000000
Ticks: 21, Total Time Elapsed: 22.0000006, Delta Time: 1E-07, FPS: 10000000
Ticks: 22, Total Time Elapsed: 23.0000002, Delta Time: 1E-07, FPS: 10000000


it says the FPS is really high, that's because I'm using the deltatime to calculate it, you can see its running at 1 FPS (which is what i set it to) from the Ticks and total time elapsed, Delta Time is really smalls (very close to 0) and it should be actually 1 since I'm running at 1 FPS

• What framework are you using? What does clock.Elapsed.TotalSeconds return and why do you need to send it to your game update if you also send the deltaTime? May 7, 2021 at 18:14
• What do you mean by "Yes I'm getting the correct target fps"? Do you mean that you don't actually have an issue? May 7, 2021 at 18:15
• @Vaillancourt the game loop is running at 10 FPS when i set the target fps to 10 but the deltatime value seems wrong and the clock is just a stopwatch class from System.Diagnostic it basically returns a double of how many seconds have passed since clock.Start(); May 7, 2021 at 18:16
• Im using .net core 5.0 with no libraries (im drawing to the console using ascii characters) May 7, 2021 at 18:17
• For the last question the GameTime class just saves the deltatime and the totaltimeelapsed so they can be used in the Update(GameTime) and Draw(GameTime) and its done by GameTime.Update(deltatime, timeelapsed) May 7, 2021 at 18:19

You're using the wrong time delta to advance your simulation.

You want "the time since I performed the last simulation step", but what you're using is "the time since I last checked if it was time to perform another simulation step". Your code is written to do a very fast busy-loop while it waits for the next time to tick, so naturally that "time since I last checked" is very very short - only the few microseconds it takes the CPU to read a value from the stopwatch, add some floats, and skip a branch that reliably predicts to false.

Your inner if should be something more like...

if (totalTimeBeforeUpdate >= TimeUntilUpdate)
{
totalTimeBeforeUpdate -= TimeUntilUpdate;

Update(TimeUntilUpdate);
Draw(TimeUntilUpdate);

Ticks++;
}


ie. if you only do a simulation step once every TimeUntilUpdate seconds, then each simulation step is TimeUntilUpdate seconds long. Since TimeUntilUpdate is a constant that doesn't change from frame to frame, this makes it a fixed time step, which is good for consistency and fairness.

Subtract TimeUntilSeconds rather than zeroing totalTimeBeforeUpdate to ensure you're correctly managing any "loose change" time left-over past the end of the interval.

In some game loops, this will be a while loop instead of an if, so you can tick multiple times to catch up (say if you had a temporary slowdown or went out of focus). Though in those cases you'll usually also have a maximum ticks condition to ensure you don't get stuck in the while forever, and the Draw call will be moved outside the loop so it triggers only after the last simulation tick of the catch-up.