I am trying to create a very simple client-server based physics game, using box2d library. A simple football game.
Obviously, the server runs only the box2d world. My question is:
is it correct to state that the client should run not only the needed rendered simulation but another box2d world as well?

I ask this because I know that the client should receive state information of the elements that have a changing state. And this information is received in packets every few ticks. And between every packet received, there's a time gap where I need to run the world like "nobody did nothing", and then correct the world state if somebody did something after I received the next packet.
That is why I need to run the box2d world in the client as well, but I feel it's kinda awkward and assymetrical.

Is this right? Is box2d prepared for this?


2 Answers 2


Is this right?

Sounds good to me.

then correct the world state if somebody did something after I received the next packet.

The simple approach is to remove the "if" here--always update everything from the server. The important state is the transform and linear and angular velocities for each box2d dynamic rigid body. You would update all these bodies on every server update.

With this approach (updating everything on every server update), you don't rely on determinism between the server and clients, which is good, because it would be difficult to get box2d to run deterministically across the server and both clients.

To improve performance on the client, you could try disabling collision detection on the client. If server updates are coming often enough (at least several per second), nobody will notice bodies briefly interpenetrating. Details:

  • Ideally, you would make your bodies shapeless, but I don't see a way to do this in box2d.
  • You could disable all collisions using collision filtering.
  • Or, you could make all your bodies kinematic. So, in between server updates, bodies will just move in straight lines (and also rotate). Their velocities won't be affected by gravity in between server updates.
  • 1
    \$\begingroup\$ You're proposing to remove collision detection.. then why not remove the physics-engine on the client altogether? Your third bullet-point (server just sending velocities) sounds like a good advice, as long as you can keep the latency low. But you don't need any physics-engine running on the client for that! \$\endgroup\$
    – bummzack
    Commented May 11, 2013 at 8:57
  • \$\begingroup\$ bummzack, the client will need to update each body's position and angle using its linear and angular velocity. (Do this every frame while waiting for the next server update.) In addition, velocities could be updated by gravity (and perhaps damping?), although the effects of these might be negligible over such short duration. This work is so simple that you're right, OP could probably code this himself rather than using box2d if he desired. \$\endgroup\$ Commented May 11, 2013 at 11:15

From my experience it's never a good idea to wait on the server to send out the world update packets before the next world update. I'd day that it'd be best if you put a copy of the whole box2d world for each client and act as if each of the other clients have the same keys pressed down, but try and send the packets to the client to update the other client's keys whenever possible. And as well update the world with these conditions. This "guessing system" works decent and updates the world without the need of the server sending them the update.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .