4
\$\begingroup\$

For 2 days now i am trying to calculate the correct delta time for my game , I am starting to getting crazy since i tried all the solutions that i found on the 5 first google pages... What is wrong? I cant get the correct delta time ,whatever i tried is just not working , the delta goes from 1 to 4 and then back 1 and then to 3 even if i take the averange delta between many frames.Plus the game runs way much faster(i mean the movement) on slow devices than in fast. The game runs on android so the spikes between frames are expected.

My code is this:

    void Game::render()
    {

        timesincestart=getTimeMil();

        _director->Render();
        _director->Update();


        float dif=(getTimeMil()-timesincestart);//usally its about 5 milliseconds

        lastcheck++;
        sumdelta+=dif;
        if(lastcheck>20)
        {
            sumdelta=sumdelta/20;
            delta=sumdelta;
            sumdelta=0;
            lastcheck=0;
        }

        LOGI("delta:%f",delta);
    }

SOLVED:

void Game::render()
{
    oldtime=newtime;
    newtime=getTimeMil();

    delta=(newtime-oldtime)/16;//divide by 16 is just making the dt closer to 1

    _director->Render();
    _director->Update();

}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ "divide by 16"? Why would you do that? If you got milliseconds and need seconds (what your comment suggests, as you want the value to be "closer to 1") then do (newtime - oldtime) * 0.001 \$\endgroup\$
    – bummzack
    Dec 5, 2012 at 7:10

2 Answers 2

5
\$\begingroup\$

i am a bit confused with your delta...

But how i do it is like this :

while( 1 ) 
{
    old = newTime;
    newTime = GetTime();

    DoMyGameAndUseLotsOfCyckels( newTime - old );
}
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I tried before something similar and simple to this but it wasn't working . I tried exactly what you said and it is working . Simplicity is the best! \$\endgroup\$
    – SteveL
    Dec 4, 2012 at 14:01
  • \$\begingroup\$ indeed it is. i was confused with this along time aswell due to everything you read on the internet. So im glad it helpt \$\endgroup\$
    – Tordin
    Dec 4, 2012 at 14:16
8
\$\begingroup\$

Read Gaffer's article, Fix your Timestep. Understand how time accumulators work. Implement them. And apply actual time delta to physics and rendering, rather than some assumed constant dt. See if that serves you better.

Because in the end, if the time delta is varying wildly on your given platform, there is nothing you can do about that but to simulate around it as described above.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Great article. When I first read that years ago, I printed it out and laminated it. It's been my goto method ever since. \$\endgroup\$
    – ydobonebi
    Oct 18, 2017 at 3:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .