How to sync physics in a multiplayer game?

JD C4M picture JD C4M · Nov 30, 2011 · Viewed 9.6k times · Source

I try to found the best method to do this, considering a turn by turn cross-plateform game on mobile (3G bandwidth) with projectile and falling blocks.

I wonder if one device (the current player turn = server role) can run the physics and send some "key frames" data (position, orientation of blocks) to the other device, which just interpolate from the current state to the "keyframes" received. With this method I'm quite afraid about the huge amount of data to guarantee the same visual on the other player's device.

Another method should be to send the physics data (force, acceleration ...) and run physics on the other device too, but I'm afraid to never have the same result at all.

Answer

Kevin Wang picture Kevin Wang · Dec 24, 2011

My current implementation works like this:

  1. Server manages physics simulation
  2. On any major collision of any object, the object's absolute position, rotation, AND velocity/acceleration/forces are sent to each client.
  3. Client sets each object at the position along with their velocity and applies the necessary forces.
  4. Client calculates latency and advances the physics system to accommodate for the lag time by that amount.

This, for me, works quite well. I have the physics system running over dozens of sub-systems (maps).

Some key things about my implementation:

Completely ignore any object that isn't flagged as "necessary". For instance, dirt and dust particles that respond to player movement or grass and water as it responds to player movement. Basically non-essential stuff.

All of this is sent through UDP by the way. This would be horrendous on TCP.