time based movement ,unstable delta calculation from sys.gettime

Hi

I have looked at this thread http://www.java-gaming.org/forums/index.php?topic=11443.0

Thats where I found the SystemTimer class, and how to use it.

This is what I do:


long delta = SystemTimer.getTime() - lastLoopTime;
lastLoopTime = SystemTimer.getTime();

System.out.println("delta: "+delta);

float oldy = gameview.getYOnMap();
float speed = 30.0f;
float bxmov = (speed * delta) / 1000; //pixels to move

System.out.println("bxmov: "+bxmov);
System.out.println("moving it: "+(int)bxmov);

gameview.setYOnMap((gameview.getYOnMap() - bxmov)); //set the new location

When I look at the console and the different delta’s it jumps alot up and down, everything from 18ms to 30ms and
this makes the movement look like many small movements then suddenly a big one…

If I just set it to move 1pixel each time without any movement timing its soo silky smooth. But since its 60fps even only 1 pixel pr frame is too fast…So
I need to time it to get slower movements.

Here’s the delta output from console:

timerTicksPerSecond: 1000
delta: 53575561
delta: 64
delta: 32
delta: 32
delta: 32
delta: 33
delta: 32
delta: 32
delta: 17
delta: 18
delta: 18
delta: 17
delta: 18
delta: 17
delta: 18
delta: 17
delta: 33
delta: 32
delta: 32
delta: 32
delta: 33
delta: 32
delta: 32
delta: 32
delta: 19
delta: 17
delta: 18
delta: 18
delta: 17
delta: 18
delta: 17

I tried to set the speed to 60, so that should be 1pixel pr frame but it still jumps up and down its not even close to how smooth it is
when I use only a static +1 movement each tick.

Anyone got any tips?

I’m not sure what the underlying code is behind SystemTimer, but you might try System.nanoTime() from Java 5.0, if only for comparision purposes.

IIRC, you have to call Sys.getTimerResolution() regularly as it recalibrates the timer. I’m not sure if this is still the case with the new 1ms timer though, or if it’s the cause of your problem at all.

I dont know that SystemTimer class, but its usual for deltas to jump around a bit. A common way to deal with it is applying some light damping (like rolling-average the last 3 or 5 values).

The thing which most likely amplified the jumpyness was printing the values to System.out. Writing stuff there is a surprisingly heavy operation. Try writing it to a file or try using some time graph.