Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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();

}
share|improve this question
    
"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 –  bummzack Dec 5 '12 at 7:10
add comment

2 Answers

up vote 3 down vote accepted

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 );
}
share|improve this answer
1  
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! –  SteveL Dec 4 '12 at 14:01
    
indeed it is. i was confused with this along time aswell due to everything you read on the internet. So im glad it helpt –  Tordin Dec 4 '12 at 14:16
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.