Sub-frame calculations


Is there any way to do calculations in OF based in a time step that is more frequent than the frame rate ? What options are there other than update() to do this ?


Hi, in setup call ofSetVerticalSync(false);
The update function will get called as fast as possible.


Thank you, but what if I want to continue to redraw the screen at 60 fps while update gets called as fast as possible ?


Take a look at threads and thread channels. you can start in ofBook


Thanks - will take a look. Not sure I need to spawn a thread to do this. I just want be able to have a procedure called from the main loop which is more frequently called than the draw method. For example, if you are calculating physics, you may have more calculation steps than the refresh rate.


@964racer Adding threads can easily become a nightmare if you need to share data between these.

The ofApp::update() and ofApp::draw() functions are both part of the main thread.
When ofSetVerticalSync(true); is called, this two functions are synced, meaning that when the screen needs to refresh update gets called and then draw, then it waits until the next refresh.
On the other hand, when ofSetVerticalSync(false); is set, the update will get called again immediately when it finishes, and when the refresh needs to be done, once the current update finishes the draw function will get called.
So, this two happen one after the other one always. If you want to calculate things more often than the refresh and then draw the results, you will always draw the results of the last update call. In the case of physics, if you need some really tight timing you might want to run the algorithm as often as possible, but otherwise it usually is enough doing it just on each refresh.

Why do you think that you’ll get a benefit by running all this calculations more often than the refresh rate?



for calculating physics in substeps what you usually do is just fixing a time step smaller than the frame time and using a loop to repeat the same calculation more than once on every update like:

void update(){
    double subFrameTime = fromPreviousFrame;
    double frameTime = ofGetLastFrameTime();
        subFrameTime += timeStep;
    fromPreviousFrame = frameTime - subFrameTime;

Then you can optimize things in the updateSystem method to use more then one thread if you need to do so by using something like openMP or just having several work queues where you can send independent tasks. For example you could have a work queue updating positions from velocities and another updating the bones in models that have a skeleton. Those two could also happen with different time steps


Thanks for the suggestion. I may employ something like that. I am thinking it would be nice to add a the concept of a timer “work proc” or “handler” to the main loop that gets called at a set frequency, which is up to the programmer. So for example, if you wanted a refresh or draw rate of 60 fps and you wanted to process 5 sub-step calculations between each frame, you could set up this timer work proc to get called on these intervals and once the proc returns, it returns control to the main loop, so the main loop is not blocked from calling other things…


what you describe sounds pretty much like ofThreadedChannel. The provided example deals with images but it could be used for anything.