const double g_dMinFrameRate = 1 / 30.f;
double g_dSlicedTime = 0.f;
double g_dUnComputedTime = 0.f;
void updateTime()
{
g_dCurTime = timeGetTime();
g_dElpasedTime = (( g_dCurTime - g_dLastTime ) * 0.001f );
g_dLastTime = g_dCurTime;
}
void gameLoop()
{
updateTime();
g_dUnComputedTime += g_dElpasedTime;
if ( g_dUnComputedTime < g_dMinFrameRate )
{
update( g_dUnComputedTime );
render( g_dUnComputedTime );
}
else
{
for ( double x = 0; x < g_dUnComputedTime; x += g_dMinFrameRate )
{
update( g_dElpasedTime );
render( g_dElpasedTime );
g_dUnComputedTime = g_dUnComputedTime - ( x - g_dMinFrameRate );
}
}
}
Delta time problem
Hi guys, Thanks for helping me with lost device & fullscreen problem. Am back here with a new problem. I use the code given below to compute delta time, Is it correct. pls help me. I am building a 2D game my anim speed varies from system to system.
EDIT: Please remember to use 'source' tags when posting code.
[Edited by - jollyjeffers on November 27, 2006 10:39:21 AM]
What language are you using?
I was checking the current time using multi threaded render loop and had probs with accessing the environment's current time. I was getting jittery movements and such with code similar to the code you posted. What I ended up doing was sampling the amount of frames rendered over a 5 second time frame and then obtained the average frames per second instead. After I did that I got smooth movements again.
Example:
also when posting code use the following syntax but replace the { brackets with [ brackets:
{source lang=c++} "code goes here" {/source}
It's easier on the eyes.
I was checking the current time using multi threaded render loop and had probs with accessing the environment's current time. I was getting jittery movements and such with code similar to the code you posted. What I ended up doing was sampling the amount of frames rendered over a 5 second time frame and then obtained the average frames per second instead. After I did that I got smooth movements again.
Example:
[source lang=c#] //time sampling used for calcing smooth time between frames used in calcs. private int timeBetweenSamples = 5000;//5 seconds per sample to adjust frame time slice. private int lastSample; private int framesSinceSample = 0; private float elapsed = 1 / 60f;//target is 60 fpspublic void ProcessMovements(){ #region Calc elapsed time int time = Environment.TickCount; int el = time - this.lastSample; //elapsed = seconds from last process if (el > this.timeBetweenSamples) { float tpf = (el / 1000f) / (float)this.framesSinceSample; this.elapsed = tpf; this.framesSinceSample = 0; this.lastSample = time; this.allowMovement = true; } this.framesSinceSample++; #endregion}
also when posting code use the following syntax but replace the { brackets with [ brackets:
{source lang=c++} "code goes here" {/source}
It's easier on the eyes.
On Windows, you can use QueryPerformanceCounter() to get a much more precise timer. On Linux gettimeofday() will probably be good enough.
The problem is that you don't clear g_dUnComputedTime after the update/render calls.
Also, you probably want to update in some fixed time step. Change the entire if() statement to something like:
This will make sure that physics runs at a predictable rate (which means that you can record a game session by just recording game input and random seeds). Meanwhile, it lets rendering run at whatever framerate the machine is capable of.
Also, you probably want to update in some fixed time step. Change the entire if() statement to something like:
double temp = g_dUncomputedTime; while (g_dUncomputedTime >= g_dMinFrameRate) { update(g_dMinFrameRate); g_dUncomputedTime -= g_dMinFrameRate; } render(temp);
This will make sure that physics runs at a predictable rate (which means that you can record a game session by just recording game input and random seeds). Meanwhile, it lets rendering run at whatever framerate the machine is capable of.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement