bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.
Apache License 2.0
2.35k stars 272 forks source link

Accumulated impulse scaling helper for timestep changes #46

Closed RossNordby closed 5 years ago

RossNordby commented 6 years ago

Impulses applied by constraints are instantaneous. In order to counteract a force of X with a time step duration of 1/60, a constraint has to apply an impulse of X/60 each time step.

Impulses are remembered from frame to frame for warm starting. If the time step duration changes significantly, then the previous frame's result will no longer be a good guess. Consider what happens in the example above when the next frame uses a time step duration of 1/180: the impulse is three times too big.

Updating the cached accumulated impulses isn't free, so ideally we wouldn't do it every frame under the assumption that the time step has changed. Instead, expose a function to do it explicitly. Could implement it on a per-set basis to let the user choose whether to apply the changes to inactive objects as well.

RossNordby commented 5 years ago

Added a Solver.ScaleAccumulatedImpulses function. It's not ideal- it does not use multithreading and doesn't get applied during warm start (which means it's just wasting bandwidth).

This is a bit of a tricky issue due to inactive constraints. There can be ridiculous numbers of inactive constraints relative to active constraints, so updating them every time can be expensive. And there's no way to bundle it into the warm start since inactive constraints aren't warm started. Maybe cache the accumulated scale and delay until awakened, but that's really complicated.

Given level of complexity, low value, and standing recommendation to 'just use a fixed time step duration', I'll treat this as done enough for release.