Running physics separately from the main game loop #131
-
Hello! I've got another question. I've seen a few references around running the physics engine separately from the main game loop at fixed rate and it's something I'm considering for integration as well. Although I'm leaning more towards doing it in sync with the main game loop instead, I want to get a bit better understanding on how some things work or how they would be approached for something like this if that's ok! If it was running fully separately, the body transforms have to be interpolated or extrapolated to ensure they remain smooth with differing tick rates. With the low level design of BEPUv2 I figure this is completely on the integration side. I remember BEPUv1 had some helpers for buffering the states, but I don't think that's present here. However one main thing I'm not certain if I understand correctly are queries - raycasts and sweeps. Those typically happen from the main game loop, but with the asynchronous run, the physics can be mid-update, so I assume it's not safe to execute them this way. Would the correct approach there be to collect all the raycasts and sweeps in the game loop and then schedule to run them in the physics update loop and then sync the results back with the main game loop? Or is there a way to run the queries safely from another thread, even if the physics engine is currently executing a timestep? I'm just trying to get a more general understanding, so I don't need too many details on the implementation, but more just trying to get a good "angle of attack" for these things if that makes sense! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Correct.
Yup.
Yup, some kind of message passing and deferred handling would probably be best. Given that simulation and game logic tend to interact a lot, I typically advise against running them asynchronously from each other. It's doable, but there's all kinds of annoyances involved like this, with tons of opportunities for difficult to detect and debug oopsies. If the goal is to decouple rendering rate from simulation rate, I usually recommend making the cut on the graphics side. The accessed surface from game logic to graphics is usually smaller than game logic to physics, or at least can be made smaller. For what it's worth, in my internal projects, I've come to favor splitting things into three asynchronous timestreams:
|
Beta Was this translation helpful? Give feedback.
Correct.
Yup.
Yup, some kind of mes…