2
\$\begingroup\$

I'm using C# Box2D library for my server and client, they have similar world.step(), iterations and update function. At the same time they have different physics' results, difference is not fixed, it may be 1-2% or more than 10%. My timestep is 0.02f and 10 iterations(for both). Code of the client and the server is almost the same. I believe it's because of I have the same timestep on both, server and client, but their code execution speed may be quite different. How can I get the same results in different applications/on different computers?

\$\endgroup\$
  • 1
    \$\begingroup\$ According to the Box2D FAQ: "People often want to know if Box2D can produce identical results on different binaries and on different platforms. The answer is no." So this is not a promise that Box2D makes out of the box. \$\endgroup\$ – DMGregory Mar 14 '19 at 19:33
  • \$\begingroup\$ But how do others simulate physics on different platforms and get similar results then? \$\endgroup\$ – Basea Basilia Mar 14 '19 at 19:37
  • 3
    \$\begingroup\$ They most likely have a master which sends authoritative data to the clients which use it to correct their local simulation. \$\endgroup\$ – Vaillancourt Mar 14 '19 at 19:42
1
\$\begingroup\$

Getting two physics systems to do exactly the same thing is a very difficult problem that requires having the physics simulations be entirely deterministic. Even incredibly tiny differences between the two will accumulate into wildly different outcomes given time. Searching about Box2D determinism it seems that you can't count on that in this case due to differences in how different processors handle their floating point math.

That being said, you say that the simulations are still relatively close, so what you can do is allow the client to run its simulation, but periodically have the server send state to the client for it to update its model. Doing things this way does mean that the client's version of things will differ in small ways a fair amount of the time so you can't rely on anything the client system says happened until the server verifies... but really for a networked game that is already the case.

The key is going to be to find a happy balance between fluidity (client simulated) and correctness (server simulated) so the game feels as good as possible on all sides while still having all the meaningful gameplay elements in the right places. If you've got purely decorative elements like physics based particle explosions that don't move gameplay elements, feel free to not even bother syncing those and let the simulations fly!

| improve this answer | |
\$\endgroup\$

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.