SuperQuadric Ellipsoids and Toroids,
OpenGL Lighting, and Timing
by Jonathan Metzgar
TimingTiming is a critical part of developing simulations. Many simulations suffer from this because they don't have support for controlling the speed of their program. Here is a sample implementation of a timing loop in Win32. In this tutorial's program, we have defined a few variables, DTick, Tick1, and Tick2. These are used to store the delta time, initial time, and final time, respectively. These are defined globally so they can be used by the entire program.
Next we have to "prime" the variables so we do this at the very beginning of our program in WinMain. We use the GetTickCount() function to return the number of milliseconds since the computer was started.
Now we go to our loop and do the same calculation except that we replace Tick1 with the value from Tick2 so we can calculate the difference in time since the last frame. We get the delta time by subtracting the two and finally divide by 30.0 to get a factor for updating our variables.
That's all you need and it should keep your simulation running smoother when the computer is faster. On slow computers, it will keep the frame up to date to ensure accuracy. If you have any questions, please free feel to e-mail me at microwerx@yahoo.com. You can also visit my website at http://www.geocities.com/microwerx/ for more information, games, articles, and more!
|