#DeltaTime is zero

42 messages · Page 1 of 1 (latest)

lime yoke
#

This is my game engine's game loop.

Initialize();

Timer timer{};
while (window.IsRunning())
{
    timer.Tick();
    timer.Reset();

    Update(timer.GetDeltaTime());
    Render(timer.GetDeltaTime());

    window.Update();
}
Cleanup();

These are my Timer class code.

Timer::Timer()
{
    Reset();
    m_DeltaTime = std::chrono::duration<float>(0.f);
}
void Timer::Reset()
{
    m_StartTime = std::chrono::high_resolution_clock::now();
}
float Timer::GetDeltaTime() const
{
    return m_DeltaTime.count();
}
void Timer::Tick()
{
    m_DeltaTime = std::chrono::high_resolution_clock::now() - m_StartTime;
}

And I found that my deltaTime is zero! I have no idea why this is happening. Help

nimble swanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

steep elbow
#

did your rember to incrent it

lime yoke
#

Increment what?

steep elbow
#

the time

lime yoke
#

Do I have to incrent the time?

steep elbow
#

i tink

#

probaly

lime yoke
#

I don't understand

steep elbow
#

increment the time by the delta

lime yoke
#

why?

steep elbow
#

so that it goes up

violet ether
#

probably looping too fast to have anything meaningful in the float

lime yoke
#

So I tried Sleep(1000); in Update(float) but still dt is zero

violet ether
#

what type is m_DeltaTime?

lime yoke
#
        std::chrono::high_resolution_clock::time_point m_StartTime;
        std::chrono::duration<float> m_DeltaTime;
azure peak
#

durations should only be signed integers

#

floating point doesn't work

lime yoke
#

WHAT? I really didn't know that

violet ether
#

really?

azure peak
#

try replacing the type of m_DeltaTime with std::chrono::milliseconds

violet ether
#

they use one on cppref as an example and it looks to work fine

azure peak
#

let's conveniently ignore that trolol

violet ether
#

I do agree though that std::chrono::milliseconds would be the better choice here

azure peak
#

seriously though you don't need to define your own duration here

#

yeah the standard library already has a definition for you

azure peak
#

it just has to be an arithmetic type so float work

lime yoke
#
    void Timer::Tick()
    {
        m_DeltaTime = std::chrono::duration_cast<std::chrono::milliseconds>(m_StartTime.time_since_epoch());
    }

I tried this, and I think it's still zero

round current
#

yes float works but keeping track of time using a float is probably a bad idea

violet ether
#

especially for a game loop

round current
#

is zero because not enough time passes between the points where you're taking the time

azure peak
#

yeah there's no code between Reset and Tick in the game loop

lime yoke
#

Ah I see

round current
#

also, you'll probably want to use steady_clock, not high_resolution_clock

lime yoke
#

Then I gotta edit the loop to

Initialize();

Timer timer{};
while (window.IsRunning())
{
    timer.Tick();

    Update(timer.GetDeltaTime());
    Render(timer.GetDeltaTime());

    timer.Reset();
    window.Update();
}
Cleanup();
round current
#

yeah that makes a lot more sense

lime yoke
#

Finally it's not zero

#

Thank you all

round current
#

do note that accumulating tiny deltas is probably not the best way to deal with time

#

but oh well

lime yoke
#

!solved