This isn’t a tutorial, it’s more of a record of what I’ve discovered when attempting to network Box2D using Apple’s GameKit networking.
Basically I took my game template tutorial and mashed in the networking tutorial to see what would
happen. The ultimate goal was to be able to add network multiplayer to iSoccer. I was pretty close to success however the physics sync jitters more than I think is acceptable for a production iOS App. It may be good enough for your purposes however so I suggest trying it out anyway. Perhaps you can suggest a better way, I’m really keen for one!
Download the Networked Box2D Xcode ‘Testing’ project from here. If you’ve used my other testing projects please ensure you click Product , Clean in Xcode to remove any conflicts.
Network Game Ground Rules
After reading this article I can surmise that I should:
- Use UDP
- Send as little data between devices as possible
- Implement client side prediction (i.e. run the physics simulation separately on each device and only sync forces that change the simulation on each device)
- Send over the game state every now and again as a true-up
How’d that go?
Not great, although GameKit uses UDP so hooray for at least one thing.
Based on the rest of the ground rules I determined it would be a good idea to start the game on each device in the same state. That is, the same physics starting point. That begs the question – how do we sync two Box2D states on separate devices? The answer direct from the creator of Box2D is you can’t. Sad face. Part of the reason is that you can’t guarantee each device will produce the exact same simulation as the CPU and runtimes may differ. There is no network support for Box2D.
Maybe Client / Server?
Next step was to say well fine we will have one device do all the work and just send position info to the other device. You can see the results for yourself in the downloaded project. Close but no cigar as they say.
I’m still looking for a solution to this problem so if you have any tips send me an email!