Advertisement

Improving Timing, Need a Hand.

Started by September 06, 2000 08:54 PM
8 comments, last by Peddler 24 years, 3 months ago
Hey, I am trying to get the best timing technique I can so that my game will run smoothly on all(most) computers. Right now, I am using a kind of Dynamic Frame Rate, where for each object I measure the time it took to complete a frame and then depending on that time I move a certain amount, so that the object will move the same on all computers...but...I am not getting the smoothest motion I would like. Here is an example of what I have now.
            
if (Object->GetTimeStamp() != 0)
{	
  unsigned long newtime = timeGetTime();
  float frames_per_second = float(newtime - Object->GetTimeStamp()) / 1000.0f;
  Object->SetVelocity(260);
  
  Object->SetPosX( Object->GetPosX() + int(Object->GetVelocity() * frames_per_second) );
  
  Object->SetTimeStamp(0);
}
if (Object->GetTimeStamp() == 0)
{
  Object->SetTimeStamp( timeGetTime() );
}
         
Does anyone have any suggestions on what I could do to ensure the best possible timing, so that the motion is smooth. Thanks -Tim Yarosh Edited by - Peddler on 9/6/00 8:59:16 PM Edited by - Peddler on 9/6/00 8:59:51 PM
I suppose you could lock the frame rate at a constant 30 or 60 frames per second. Would this be feasible for your application?

Advertisement
I guess I could try that, maybe as a supplement to what I already have. What is the best way of going about locking a set framerate?

quote: Original post by Peddler

I guess I could try that, maybe as a supplement to what I already have. What is the best way of going about locking a set framerate?



Don''t.

The trick here is to separate your rendering and game logic systems. You will want your game logic to run at a certain frequency. You also want to use any unused cpu time to render, thereby getting the smoothest framerate possible.

You could do it like this:

while (1){    timer = getCurrentMillisecond();    if ((timer - lastLogicTimer) % LOGIC_FREQUENCY == 0)    {        doGameLogic();        lastLogicTimer = timer;    }    render();} 

timGetTime doesn''t have a good resolution use performance counter instead....
Sorry don''t remember how while i''m at work now ....
FordPrefect, What is the LOGIC_FREQUENCY variable set to? The desired Frame Rate?

-Thanks
Advertisement
quote: Original post by Peddler

FordPrefect, What is the LOGIC_FREQUENCY variable set to? The desired Frame Rate?

-Thanks


In this example, LOGIC_FREQUENCY would be set to the number of milliseconds between game logic frames. For instance if you wanted your logic to run at 30hz, you''d set it to (1000 / 30).

Actually, now that I''ve looked over my code, it''s not necessary to keep track of the time of the last logic frame at all. Oh well, such are the dangers of writing code at 1 A.M.
Fordperfect -

Why is it not necessary to keep track of the last logic frame? What method do you use then?
There is no spoon.
I hate to sound stupid... but is getCurrentMillisecond an actual function? If not, what timer function would you guys recommend, besides the windows timer?
Use a Multimedia Timer to constantly calculate the sprite positions to say 100 times a second.. its much easier to do collision detection this way, and its more accurate. Using an MMTimer leaves more fire power for the Graphics system.

Ive tried this on a Pentium 90 with good results.

  Downloads:  ZeroOne Realm

This topic is closed to new replies.

Advertisement