Sign in to follow this  

Delta time problem

Recommended Posts

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.
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()
	g_dUnComputedTime += g_dElpasedTime;
	if ( g_dUnComputedTime < g_dMinFrameRate )
		update( g_dUnComputedTime );
		render( g_dUnComputedTime );
		for ( double x = 0; x < g_dUnComputedTime; x += g_dMinFrameRate )
			update( g_dElpasedTime );
			render( g_dElpasedTime );
			g_dUnComputedTime = g_dUnComputedTime - ( x - g_dMinFrameRate );
EDIT: Please remember to use 'source' tags when posting code. [Edited by - jollyjeffers on November 27, 2006 10:39:21 AM]

Share this post

Link to post
Share on other sites
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.


[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 fps

public 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;


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.

Share this post

Link to post
Share on other sites
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:

double temp = g_dUncomputedTime;
while (g_dUncomputedTime >= g_dMinFrameRate) {
g_dUncomputedTime -= g_dMinFrameRate;

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this