3
\$\begingroup\$

I have a particle system with a physics simulation integrator based on delta time (elapsed time between frames) which is implemented on the GPU in a compute shader. I also have a frustum culling system implemented on the GPU. The frustum culling gives me the possibility to cull away draw calls and the dispatch calls for the particle simulation. The culling of draw calls works well, I also don't have a problem with culling the particle simulation when the camera is looking away.

My problem happens when I want to catch up the particle system simulation when the camera looks back at the particle system after looking away for a while. What I do is accumulate the time passed while looking away, then I resume the simulation using all that accumulated time. The problem with this approach is that if the simulation is anything but a simple constant velocity change in one direction, the simulation just doesn't look the same as if the simulation was playing one frame delta at a time.

From what I have read this is a common issue with using time elapsed between frames to simulate physics. What I wonder is if there's a different approach that can actually allow me to correctly play back many seconds of simulation (let's say 20 seconds) the same as if it was played one frame delta at a time?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That depends on what exactly you are simulating. For example, when you have a particle system where particles attract particles, then extrapolating that systems state is an np-complete problem which can not be solved in any way except a step-by-step simulation. \$\endgroup\$ – Philipp Dec 10 '20 at 9:01
  • 1
    \$\begingroup\$ As Philipp says, there's no one-size-fits-all solution here. But there are solutions to many special cases. To get good answers specific to a case of interest for your game, we'll need more details from you about what one, specific effect you want help fast-forwarding in this way. If the answers you get for that effect don't work for the next, different simulation effect you want to fast-forward, then you can ask a new question about that effect next. \$\endgroup\$ – DMGregory Dec 10 '20 at 12:32
1
\$\begingroup\$

Look up how Unity‘s FixedUpdate solves this: it’s a commonly used mechanism you might take inspiration from. FixedUpdate is called a fixed number of times per second; you decide how many by setting the fixedDeltaTime value in settings, which by default is 0.02s. Unity will always run this 50 times per second. Doing so any calculations inside the FixedUpdate should be accurate. This is necessary because whatever real-time system you’re developing, you must account for occasional frame drops and your physic simulations must not depend on time deltas.
It’s not too hard to implement the same mechanism in your own system. You can even do it in a separate thread.
Side note: FixedUpdate, as it runs many times in one frame, can easily become heavy so it’s good to make a minimal use of it.

\$\endgroup\$
4
  • \$\begingroup\$ Where does the question mention Unity? \$\endgroup\$ – Philipp Dec 10 '20 at 8:58
  • \$\begingroup\$ @Philipp is it not allowed to mention a software as an example now? \$\endgroup\$ – SteakOverflow Dec 10 '20 at 9:04
  • \$\begingroup\$ If I've been looking away from the particle system for 3 seconds, then look back, this would entail stepping that system's simulation ~150 times in one frame, no? Do you have any recommendations for how to mitigate this kind of sudden cost spike? \$\endgroup\$ – DMGregory Dec 10 '20 at 13:20
  • \$\begingroup\$ being aware of the cost spike is the first step. There’s no method other than doing the absolute essential minimum for physics when you prioritise precision over resource efficiency. That said you can adjust the fixed delta time and find a balance between the two. Example: at 0.1 FixedUpdate would run 10 times/s; decide that value based on how precise you need to be. BUT... as @Philipp commented, particle systems can be a big issue due to the nature of the problem. For a simple particle system without any internal interactions this approach will work. \$\endgroup\$ – SteakOverflow Dec 10 '20 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.