Writing

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label Timers. Show all posts
Showing posts with label Timers. Show all posts

Timers: Part Two

Timers:
Part One | Part Two


Lets start with a review of Part One.
  • Any increments of 10 ms jitters Java on Windows.
  • High frame rates are not as important as smooth graphics
  • The loopGame delay smoother must never be negative.

With this in mind, I've decided to build a simple but real game called "Subby and the fishes." For the beginning of my development, I was setup on my desktop PC. With my delay smoother set to 21 milliseconds and a hefty graphics card, a Radeon 3800, everything ran smoothly.

Once I moved the code over to my laptop with the Intel video chipset, my delay smoother started hitting negative numbers again. My output was 21, 6, 6, -7, 21, 21, 6. I watched the screen and sure enough the bubbles I had floating up on the splash screen jittered. Bah!

I had to up my delay up to 31 milliseconds. Note I couldn't do 30 because of the Java bug that causes more jitter.

long sleep = loopTime+31-System.currentTimeMillis();
System.out.println(sleep); // check to make sure never negative
try { Thread.sleep(sleep); } catch (Exception e) {}

Ok now I viewed the screen. Still jittering...what the hell.

I checked to make sure my animation was using the smoother. Yes, my move smoother was using the timeLapse variable of the game . Now, when I thought about it, something didn't make sense. Since my game loop was smooth, in other words, never negative, that meant that the time lapse variable I sent to move things should be pretty darn close to constant.

delta = (int) ((timeLapse * speed) / 1000);
System.out.println(delta);

It should be constant but it wasn't. It was all over the place with an output of variance of 6 pixels. Wow, it was terrible. Time to research. Hours later I found David Holmes blog.

http://blogs.sun.com/dholmes/entry/inside_the_hotspot_vm_clocks

Okay it has a ton of information and I'm thankful for it. It just strikes me that if Java was serious about becoming a major gaming platform they would come up with a better than C++ solution for timers. One can only dream right?

So what in the article is useful? I decided to try to force high resolution and recheck my movement delta.

--xx:+ForceTimeHighResolution

My output was perfect. 14, 13, 14, 13, 14. All the movements were within one pixel of each other and looked much smoother.

So for the time being a delay of 31 milliseconds and forced high resolution timer seems to be good enough of my lap top. I really want to test on some even slower machines to make sure my smoother delay is large enough but I'm going to wait until the game is more complete.

NanoTime

I wrote a nano timer as well but had the same poor results. Since the resolution of the the thread.sleep is in milliseconds I think milliseconds are probably the best resolution you can hope for. I also found the manipulation of nano time more than a little annoying. It was very hard to tell how many zeros in the code. 1,000,000,000 is fairly easy to read but 100000000 is not--note the error. Writing 1000*1000*1000 seemed awkward.

I also made a half hearted attempt at using the delay. However, I would have to set up my animation code quite a bit differently to use this. At first brush, it seems like this would make my code simpler but it is not the way I've implemented my animations. Perhaps in the next game I'll try this loop from the outset.

I've tried GageTimer as well as per Kevin Glass's recommendation. However, I found it just as varied as writing my own.



Notes and Links:

Here are some things I'm still trying to organize.


http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime
Need to figure this out.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6435126

Timers: Part One

Timers:
Part One | Part Two

I've had to learn more about timers than I really wanted to. My testing gave quite confusing results, so I had to dig into the issues and was not very satisfied with the answers. The bottom line is the resolution of Java timers is not very accurate because Java doesn't play well with Windows processor in a multiple processor architecture. Mac and Linux are fine, even with multiple processors, according to many other game programmers. It seems that Windows will sleep processes in different areas of the processor or even in different processors effectively blacking out the timer so Java does not know how much concurrent time has passed. Estimates seem that these blackouts can vary wildly from 2 to 20 milliseconds. This causes both inaccurate FPS reporting and jitters.

100 FPS and the Jitters

My basic millisecond counter counts the number of times the game loop loops in 1000 milliseconds or one second. To display smooth animations and movements, we need movement and frame rate to be constant over time. The easiest example is movement. Lets say I want to move a sprite across the screen 10 pixels every 20 milliseconds. In a perfect world, it would look like this. The velocity, rate times time, is constant and smooth to the eye.

Time: 20 40 60 80 100
Move: 10 20 30 40 50

Now if there is a delay in the game loop due to processing requirements or a poor resolution timer, the velocity become jittery as well as change speeds, either increasing or decreasing.

Time: 15 50 80 95 130
Move: 10 20 30 40 50

In the above case, the animation not only stutters but arrives late to the destination point. In a perfect timer world, this can be smoothed out by sleeping the game loop for constant amount of time. Mark the time at the very beginning of your game loop. Subtract the amount of time at the end of the game loop. This will give you a negative number. Add a delay that is greater than the negative number and viola you have smooth graphics. The game loop is always performed at the same time.

long sleep = delay+ loopTime-System.currentTimeMillis();

Now if my sleep interval is negative, I start getting the jitters on the display. If I don't put any sort of delay in at all, I can get 500 FPS or more but here come major jitters. Now you will notice if I'm always sleeping for 30 milliseconds for every loop with a positive sleep number calculation, my FPS will be around 33 FPS and never more. 1000/30 = 33.

My dreams of 100 FPS are starting to fade. The only way I can achieve 100 FPS is if my delay is set to 10 milliseconds. 1000/10 = 100. That means for any amount of processing, map rendering, sound, path finding, my PC only has 10 milliseconds to calculate it all. I did a quick test on my home computer and found some bugs in Java. I went to 9 milliseconds and it worked fine with a 1000 sprites. However on my lap top, even with very few slights I was starting to see a jitter of +/- 15 milliseconds. So the minimum delay for my lap top is about 20, depending on what other slow downs my engine introduces.

On a positive note, I did some research and the human eye does pretty well with anything around 18-24 FPS similar the the speeds of video and film. Of course there are a lot of exceptions on how the eye works in the real world like blurring which is not possible on a monitor. One interesting thing that seems pretty important is refresh rate of the monitor versus what is displayed on the screen. The eye actually becomes tired when these two things are different. For example if your screen is refreshing at 72hz or 72FPS the optimum for the game would 72 as well.

Assuming that your game can run smoothly without jitter, the best way to set your frame rate would be dynamically at the screen refresh rate. Another way to approach this, would be to test your worst processing moments (when your game loop is slowed to the absolute maximum amount for that pc) and then set your delay to a value greater than this maximum processing delay by a few milliseconds. It would be very cool to do this dynmanically. Something like set to delay to whichever is worse, process delay or screen refresh rate. At some point I may try to implement this, depending if Java 2D gives me enough wiggle room.

In the mean time, my desktop pc runs fine at 9 millisecond delay, even with a thousand sprites. For fun I tested 10,000 sprites and could still set a delay at 25 before going negative. Note I will discuss the Java/Microsoft bug of 10 milliseconds increments in the next part. However on my laptop 9 milliseconds gives me a negative sleep number. At 20 delays were 4, 4, 4, 4, 3, 4, -5, -5, -5, -6, 3 6 per loop. For even a pretty small amount of blitting I had to set delay to 30 to avoid negatives, pushing my FPS down to 33.

Bollocks! My dream of 100 FPS is gone. But games are vain. How a game looks is extremely important to gamers. My new mistress is smooth, sexy graphics. With that in mind, I'm happy to divorce my original goal of speed at any cost. We do have a new rule though.

Rule: Sleep must be positive for all target machines.

I have a feeling that this is going to be hard to figure out when a game is completely flushed out. I think the maximum delay that I would like to go would be 50 which gives 20 fps. I really prefer though to keep the FPS as high as possible, as close to the refresh rate as possible.