Snapshot and rollback state? #202
-
I am working on prototyping implementing Rollback netcode (https://en.wikipedia.org/wiki/Netcode#Rollback) in a multiplayer 3D FPS game. This requires that multiple entire game and physics states are able to be stored temporarily and resumed. Obviously this would impose some pretty big limitations on how complex the game state and physics can be but please presume something pretty simple. What is the feasibility of efficiently snapshotting and resuming the entire physics state using BepuPhysics? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There are multiple levels this could be implemented at, with increasing levels of difficulty. The easiest would be to simply copy body poses and velocities for all bodies, and upon rollback, just copy those states. There's a little bit of extra complexity in tracking changes- perhaps a body has been added since the rollback target; that'd need to be explicitly tracked too. To keep things simple, you'd probably want to wake up every body that has its state set so that all the other internal state (like collision constraints) gets updated appropriately. Another step would be to copy accumulated impulses for all constraints. These are guesses at the solution to constraints that the solver keeps between solves, so giving the solver the same guess as it had originally would help produce similar results. Another step would be to try to preserve sleep states. This is a little tricky, since bodies wake and sleep in groups that depend on constraint connectivity. Simply moving a body will not immediately create a collision constraint if it gets moved into collision; the simulation has to run a timestep to determine that. You could store the sleep state and just force bodies asleep or awake as appropriate, and it'll turn out... fairly close to correct. It won't be the same sleep islands, and the manner in which a rolled back pile of bodies wakes back up could be a little odd (possibly a propagating wave of wakefulness, rather than all waking up at once, due to the lack of constraints- usually won't be noticeable since the rollback won't be THAT far back in time). The ultimate form would be to clone the entirety of the internal state of the library. This would entail copying the contents of every buffer in every BodySet and ConstraintSet (and every sub-buffer within the constraint batches), plus all the buffers in the broad phase, and so forth. If you did this, the result would be a perfect bit-for-bit replication of the original state, and provided There's aren't any APIs in the library to do this deep copy for you, but all (or almost all? I might be forgetting something) of the information is exposed, so in principle it would be doable. It's something that I've wanted to support out of the box for a while- it's useful for a lot of stuff, from rollback to serializing full simulation state for debug snapshot purposes. Unfortunately, it's still probably going to be a while before I get around to it. |
Beta Was this translation helpful? Give feedback.
There are multiple levels this could be implemented at, with increasing levels of difficulty.
The easiest would be to simply copy body poses and velocities for all bodies, and upon rollback, just copy those states. There's a little bit of extra complexity in tracking changes- perhaps a body has been added since the rollback target; that'd need to be explicitly tracked too. To keep things simple, you'd probably want to wake up every body that has its state set so that all the other internal state (like collision constraints) gets updated appropriately.
Another step would be to copy accumulated impulses for all constraints. These are guesses at the solution to constraints that the solver ke…