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.

[3829 byte] By [westonbeecroft] at [2007-9-30 8:59:35]
# 1

1) sleep is not millisecond accurate,.

Have a look at the GageTimer (http://java.dnsalias.com/) the algorithm in that is how most ppl do their timing. (while loop waiting checking nanoTime, with a Thread..yield() inside the loop)

2) if(bufferStrategy.contentsLost())

{

System.out.println("Contents was lost");

}

else if(bufferStrategy.contentsRestored())

{

doRendering();

System.out.println("Contents Restored");

}

else

{

doRendering();

}

You have misunderstood the usage of contentsLost/Restored, to avoid the screen rendering a white frame now and then your rendering section should look something like this :-

while(true)

{

doRendering();

if(bufferStrategy.contentsLost())

{

while(!bufferStrategy.contentsRestored());

}

else

{

break;

}

}

bufferStrategy.show(); //this has to be moved out of the doRendering() method.

Abuse at 2007-7-2 20:47:41 > top of Java-index,Other Topics,Java Game Development...
# 2

Thanks for the tip on using BufferStrategy, I thought I was doing something wrong which is why I included it in my code piece heh heh. Ok, I am now using the Gage timer, my framerate still fluxuates, but I am guess that is normal since I'm not averaging it or anything and at least it now stays the same when heavy calculations take place. Since my framerate remains the same but the screen is still a bit jerky when scrolling, I am going to have to guess that the issue is with the screen not repainting fast enough. Another guess here, perhaps using page flipping could solve this... I can't seem to find any information on how to actualy do page flipping, I have a feeling that it is accomplished in some subtle way that I am missing.

westonbeecroft at 2007-7-2 20:47:41 > top of Java-index,Other Topics,Java Game Development...
# 3
maybe it has something to do with your scrolling. If you want it really smooth, and you're using doubles, only cast to int when you're going to draw. Other than that, remove all rounding to eliminate jerking that can cause.
Malohkan at 2007-7-2 20:47:41 > top of Java-index,Other Topics,Java Game Development...
# 4

> 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.

You said that your program is using more processing power when your map is scrolling, and that you are subtracting the time it took to go through your main game loop from your pause time... what if while scrolling, the time it takes to go through your loop is greater than or equal to your pause time? You would not sleep at all, and your game would get choppy.

Bread_Stick at 2007-7-2 20:47:41 > top of Java-index,Other Topics,Java Game Development...
# 5
nope, thats not it. It uses maybe an extra 10% cpu while scrolling.
westonbeecroft at 2007-7-2 20:47:41 > top of Java-index,Other Topics,Java Game Development...