Jerky Animation Problem
When I scroll my map there is a point where many calculations must be done and this takes longer than the usual run through my game loop. This is my first time using a main game loop (I used threads before), and I was assuming that by subtracting the time it took to go through the loop from my pause time and then sleeping for that pause time, I would be able to maintain a constant speed despite minor fluxuations in time required to process the loop.
My game is using about 10% cpu when it is not scrolling and it jumps to about 18% or so when it is scrolling, I need my game loop to be able to adjust for this difference so the animation is not jerky, here is some code so you can see what I'm doing:
PrecisionTimer timer =new PrecisionTimer();
while(!stopped)
{
startTime = timer.currentTimeMillis();
for (int i = 0; i < users.size(); i++)
{
Actor actor = (Actor)users.elementAt(i);
moveActorX(actor);
moveActorY(actor);
}
checkForScrolling();
if(bufferStrategy.contentsLost())
{
System.out.println("Contents was lost");
}
elseif(bufferStrategy.contentsRestored())
{
doRendering();
System.out.println("Contents Restored");
}
else
{
doRendering();
}
fps.frame();
elapsedTime = timer.currentTimeMillis() - startTime;
if(elapsedTime >= SLEEP_TIME)
{
elapsedTime = SLEEP_TIME - 1;
}
try
{
sleep(SLEEP_TIME - elapsedTime);
}
catch(Exception ex){ex.printStackTrace();}
}
}
The other important piece of information would be the timer I am using, my timer object is based on the sun.misc.Perf timer, my code for using this is as follows:
import sun.misc.Perf;
publicclass PrecisionTimer{
Perf hiResTimer;
long freq;
public PrecisionTimer(){
hiResTimer = Perf.getPerf();
freq = hiResTimer.highResFrequency();
}
publiclong currentTimeMillis(){
return hiResTimer.highResCounter() * 1000 / freq;
}
}
Once again, what I am looking for is a method of adjusting my pause time so that it accurately pauses for less time when it takes longer to go through my game loop. The other problem with this is that I am unable to maintain a constant frame rate, it is very volatile, switching from anywhere between 50 and 60 fps even when nothing is going on in the game.

