Page 1 of 1

Posted: 17 Dec 2017, 17:59
by oli_lab
Hi !
I'm trying to build a physics engine for a new module,
most of the updates are done inside callback (record, play, mouse, inputs...)

but still the physics should be computed regularly even so there is no callbacks called back.

so some computation should be done in the onProcess() loop

what would be the best option(s) to have the physic engine tamed to reasonable speed ?

- just leave it as it is in the onProcess to be computed each time onProcess is called ?
- use samplerate and blocksize to have the computing done once every "n" blocks ?
- using system time ?

if someone is willing to share some code that just does that, it will be cool !

thanx

Olivar

Posted: 17 Dec 2017, 21:40
by sm_jamieson
If you don't need it calculated every bloc then to do so is a waste of CPU so I would not do that.

If you use system time by checking it in every bloc, its more efficient to count blocs since it uses less CPU instructions.

You can use sample rate and bloc size to work out how many blocs make up the time interval, then just set a counter to the value and subtract 1 every bloc until the value gets to zero, then do the action and reset the bloc counter.
An example of counting blocs with interval approximately 0.1 seconds:

void ModuleClass::onInitModule (MasterInfo* pMasterInfo, ModuleInfo* pModuleInfo)
{
double interval_secs = 0.1; // Use double data type since sdkGetSampleRate returns double

int numblocs = (sdkGetSampleRate() / (double)sdkGetBlocSize()) * interval_secs; // "int" numblocs to truncate result

counter = numblocs;
}

void ModuleClass::onProcess(void)
{
if (--counter == 0) {
doAction();
counter = numblocs;
}
}

This is not always accurate due to rounding off to get an exact number of blocs. If you need it to be accurate on average over time, you can keep track of the truncated fraction and use it to adjust the bloc count.

If exact timing is required (perhaps part way through a bloc), a timer might be required (thread timer / windows timer, etc). If you use a callback timer the callback might be in a different thread so you might need thread protection (mutexes, etc.)

Hope that helps.

Posted: 17 Dec 2017, 23:13
by 23fx23
yes do kinda same here, increment a counter in process, and past max_count it resets, to 0 trigg the action, max count being defined relative to blocsize

Posted: 17 Dec 2017, 23:38
by oli_lab
thank you !
I'll try that asap. definitely going somewhere with this next module !
stay tune.

Olivar