Skip to main content
gamedev.net
🔒 Locked

Timing problems

Started by Butabee 3 replies 800+ views
Original Post
Butabee
Butabee
So I'm trying to limit my physics updating to 60 times a frame and I've accomplished that when vsync is off however when I turn vsync on, things mess up.

What I'm doing is this.

at framestart
I use QueryPerformanceCounter counter to get a start time.
I also have oldtime initialized to 0;

then I do:

if(time - oldTime > frequency / framerate)
updatephysics();
QueryPerformanceCounter(oldTime);


I don't know why but it's slowing down when vsync is turned on.

I think maybe it's because I'm calling the code when a frame is started? Instead of updating seperately from the render code?
GlaiveVehem
GlaiveVehem
Isnt it supposed to be something like this
if ( ( time-oldTime/frequency ) >= 0.016667 ) { //for 60 FPS   QueryPerformanceCounter(oldTime);   updatephysics();   ...}
myWebSite : https://sites.google.com/site/vehement04/
TomKQT
TomKQT
How exactly does your rendering code work?
If you just create a device with D3DPRESENT_INTERVAL_ONE and then call device->present, it will wait for vsync (the present call will wait). And if you're doing your physics calculations in the same thread, you get physics calculations limited ALSO to 60 FPS, with no additional work required from you. And if you try to limit it by yourself too, it can behave weird ;)
Hodgman
Hodgman
To use a fixed physics time-step, you've got to use an accumulation variable (so any time not used up in one frame is carried over to the next frame), and your updatephysics call has to be in a loop.
e.g. modifying your code --
time= QueryPerformanceCounteroldTime = 0timePerFrame = QueryPerformanceFrequency / framerateaccumulatedTime = 0while(1){  while( accumulatedTime > timePerFrame )  {    accumulatedTime -= timePerFrame    updatephysics()  }  oldTime = time  time = QueryPerformanceCounter  accumulatedTime += time-oldTime}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.