# Calculating Delta time , what is wrong?

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

}
``````
-
"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

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 );
}
``````
-
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

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.

-

A mistake that I made this week doing this :

``````while (!quit){

startTime = SDL_GetTicks();

Process Input();

Update Game();

Render();

endTime = SDL_GetTicks();

DeltaTime = endTime - startTime;
}
``````

I do not know exactly Why, But this did not work at all. the solution @Tordin gave is the best one, and I have seen it in many many many references and books. ++

Hope this might help someone else because this link pops up in google very fast when you write Delta Time C++ Game Engine;

Edit: I think this dosent work correctly, because some things are happening between startTime and endTime;

-