Timing accuracy.
Author |
Message |
facultyofmusic
|
Posted: Sat Jul 25, 2009 4:15 pm Post subject: Timing accuracy. |
|
|
Say in an animation. You draw a rectangle, and then use Thread.sleep (30) to delay for a bit before you draw the next picture. The amount of time from the first update and the second update of the position of the rectangle is very very close to 30 milliseconds.
But what if you need to draw a few hundreds or even thousands of triangles and then delay sometime before it updates again? If I use Thread.sleep (30) again, the amount of time between the first update and the second update of the position of the rectangle won't be at all 30 milliseconds, but something a lot more.
I'm stuck on this right now. In Turing there's a command called delaysincelast (milliseconds : int), which counts delay time not from when the delay starts, but from the last delay.
Is there something similar to this in Java? If not, what are some other ways of timing very accurately between animations without the influence of the amount of time it took for the objects on screen to update? I'm making a guitar hero game, and for fast songs, I need timing like this. I've tried some methods that I thought of myself, but they cause the computer to lag too much.
If there is a way (or a few ways), please please please post them! Thank you so so much!
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
riveryu
|
Posted: Tue Aug 18, 2009 12:08 pm Post subject: Re: Timing accuracy. |
|
|
What you need to do is compute exactly how much time it took from one update to the next and then decide how long to sleep.
You use System.currentTimeMillis () to get the time at moment of calling.
Hope you can understand the code below to see what I mean above.
Java: |
long before;
long after;
long timeAllowed = 30L; //30 millisecond is the time you want each iteration to last
before = System. currentTimeMillis (); //get the time before the updates
while(/*.. main runs ..*/){
//............................
//update your screen and stuff
//............................
after = System. currentTimeMillis(); //get the time after
//compute difference in time, sleep off any extra time left
sleepTime = timeAllowed - (after - before );
before = after; //after (the time this iteration) becomes the before for the next iteration
if (sleepTime < 0)//Negative means the program took more than allowed time,
sleepTime = 0; //so dont sleep at all really.
Thread. sleep(sleepTime );
}
|
HoweverI've seen a webpage saying that the Java System timer is inaccurate up to 50milliseconds and varies across different platforms and Java 3D timer is better.
I can't find it right now, but it also provided a HighResTimer class attached below. You're gonna have to figure it out though. I've never used it either. It might not even work.
If you are really interested in making your game accurate in terms of time, try and understand this article. Skip parts that aren't related to your problem(ex. it talks about physics stuff ... RK4 blah blah, just skip those).
http://gafferongames.com/game-physics/fix-your-timestep/
Description: |
I don't even know if this works. I can't find the website it came from in my history. |
|
Download |
Filename: |
HighResTimer.java |
Filesize: |
1.48 KB |
Downloaded: |
89 Time(s) |
|
|
|
|
|
|
facultyofmusic
|
Posted: Sat Aug 22, 2009 3:10 pm Post subject: Re: Timing accuracy. |
|
|
Thank you so much. It had solved my problem pretty well. I will try to understand the high-res timer thingy that you sent me. Anyways THANK YOU!!!! ^^
|
|
|
|
|
|
|
|