23.9 C
New York
Monday, April 28, 2025

2D – What kind of Delta time system is healthier?


I’m implementing a time of time and my code is presently seen (C#, however that’s irrelevant to the query):

personal static void MaybeTick()
{
    DateTime now = DateTime.UtcNow;
    TimeSpan length = now - lastTicked;
    int durationMillis = length.Seconds * 1000 + length.Milliseconds;
    uint missedTicks = (uint)(durationMillis / MILLIS_PER_TICK);
    if (missedTicks == 0)
    {
        return;
    }
    int tickOffset = durationMillis % MILLIS_PER_TICK;
    whereas (missedTicks > 0)
    {
        battleField.Tick();
        missedTicks--;
    }
    lastTicked = now.AddMilliseconds(-tickOffset);
}

A colleague thinks that as a substitute of the present implementation, it ought to accomplish that:

DateTime lastUpdateTime = DateTime.Now;

whereas (true)
{
    DateTime currentUpdateTime = DateTime.Now;
    TimeSpan elapsedTime = currentUpdateTime - lastUpdateTime;
    battleField.tick(elapsedTime.TotalSeconds);  // or .TotalMilliseconds, relying in your requirement
    lastUpdateTime = currentUpdateTime;
}

As an alternative of marking when an interval is reached, we’ll mark each time and ship the change over time by the decision chain.

For my part, that’s dangerous as a result of we have now a collision system in progress and if our items out of the blue transfer an extended distance (as a substitute of step-by-step with my methodology), they might graduate one another as a substitute of colliding. My colleague says that I solely use the continual detection of collisions, no matter.

What path is healthier and why?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles