[Solved] Dellta time issues

Hey guys,

tl;dr: delta time change returning 0, based off System.nanoTime()

So recently I’ve been dabbling in creating a large voxel engine based game however i had my game running nicely for testing added features and what not however my clock timer randomly stopped working, and i cannot figure it out for the life of me.

Here is my main issue when i run to get delta in the game for movement gravity etc i call the getDt() method and use that for delta and at the beginning of my game loop i call the updateTimeChange() method however the delta returns 0 for every cycle.

Any pointers on how to correct this?

public class Clock {

private static float time;
private static float lastTime;
private static float dt;

public void updateTimeChange(){
	 time = System.nanoTime();
	 dt = (time - lastTime)/ 1000000;
	 lastTime = time;
}
public float getDt(){
	return dt;
}
public Clock(){
	lastTime=System.nanoTime();
}

}

You need to use long instead of float

I changed them to long, but the error still occurs, this one has me stumped.

Did you change the return type of getDt to long also?

dt = (time - lastTime)/ 1000000;

You’re truncating to millisecond accuracy.
If your update took less than 1ms, ‘dt’ will hold zero.

dt = (time - lastTime)/ 1000000.0f;

… also i would recommend using doubles instead of floats.

it might help avoiding future delta-time-issues. :wink:

There’s no compounding of errors. Doubles don’t help. Better is to skip on the divide and FP and just use raw nano info intead.

Corrected the issue by initializing the variables and changing to double.