Skip to main content
gamedev.net
🔒 Locked

Performing Calculations while Rendering

Started by shane1985 8 replies 700+ views
Original Post
shane1985
shane1985
This is my problem: When I have vsync off, my game loop runs at maximum speed, and my collision calculations are done ~150 times per second. When I turn vsync on, it slows down everything, and the calculations are only done 60 times per second, the collision calculations are less accurate. Should I disable vsync in code? Is there any way to have the rendering execute every 1/60 of a second (it should only take about 1/150), and then have the calculations done every 1/150 of a second? I would really appreciate some help with this. Thanks! -Shane
_the_phantom_
_the_phantom_
dont force v-sync on or off, leave that upto the end user, its good maners [smile]

As to your problem you could flip the calculations out to a seperate thread, but that might not be the best path, someone else would have to advise here I think [smile]

the other option is to try and make them less dependant on being updated lots and try to make them more dependant on the time things collide.
However, again, someone else would have to help you here [smile]
shane1985
shane1985
LOL, you never actually answered my question though...
you instead asked why I was asking the question, which doesn't help anyone.

I guess I could make it more dependant on the time things collide - but wouldn't that expose the same problem?

There would still exist the issue of inaccurate calculations.
shane1985
shane1985
unfortunately, the problem with that is that the vsync slows down the loop - it seems like it is waiting for the next redraw governed by the monitor refresh rate. Is there any way to tell the program that it is done drawing, so that it continues the loop?

Thanks!
_the_phantom_
_the_phantom_
same thing goes for shoving it in a thread on its own
shane1985
shane1985
Alright, are there timer functions that will allow me to execute the calculations at a speed close to 150 times per second?

Using the standard windows SetTimer() gave me a 17 ms delay at best.

Unfortunately 17 ms is way too slow, so I need a way to do it faster.

Thanks!
dazzford
dazzford
As other people said, just scale your physics over time.

If you have an object that rotates 2 radians per second, and the physics "tick" has taken 300ms then scale it accordingly eg.

OnTick(long elapsed /* in miliseconds */){  mObject.Rotate(2.0f * (elapsed * 0.001));}


Edit: oh and dont use the windows msg pump for the timer, or the "SetTimer()" functionality. There are plenty of examples of timers here on gamedev, and there are some artilces about them too. Just do a search.

Topic Locked

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

Sign in to reply to this topic.