|
Accurate timing
I am using QueryPerformanceCounter to keep my program at a framerate of 60fps, but it seems to go a little jerky. Here is the code:
Can anyone tell me how to make this code work better?
Dave2001,
Dave2999@hotmail.com
Dave2001,Dave2999@hotmail.com
Aaargh!
Trying to force your code to run at a fixed speed is a BAD idea (assuming this is under Windows or another multi-tasking OS).
What the majority of games do is update all animation (& physics, network positioning etc) based on the elapsed time since the last frame.
character.xpos += speed * time_in_miliseconds_since_last_frame;
where speed is how far the object should have moved in a single millisecond. So if the elapsed time between frames was 2 milliseconds, the pos is moved speed*2 units. speed is usually small floating point value.
Its probably easiest to think about a car:
Its speed is measured in Miles (or Km) Per Hour. From that you can work out how far it would have moved in 10 minutes - its the same principle.
If you average the elapsed time over a few frames and put in a maximum threshold, you can get very solid, non-jerky animation. (Its been a smooth enough method for the last 4 commercial PC products I''ve worked on).
--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Trying to force your code to run at a fixed speed is a BAD idea (assuming this is under Windows or another multi-tasking OS).
What the majority of games do is update all animation (& physics, network positioning etc) based on the elapsed time since the last frame.
character.xpos += speed * time_in_miliseconds_since_last_frame;
where speed is how far the object should have moved in a single millisecond. So if the elapsed time between frames was 2 milliseconds, the pos is moved speed*2 units. speed is usually small floating point value.
Its probably easiest to think about a car:
Its speed is measured in Miles (or Km) Per Hour. From that you can work out how far it would have moved in 10 minutes - its the same principle.
If you average the elapsed time over a few frames and put in a maximum threshold, you can get very solid, non-jerky animation. (Its been a smooth enough method for the last 4 commercial PC products I''ve worked on).
--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement