[Guide]How to Networking in box2d

Hello everyone,
I know this guide isn't only applicable for box2d, anyway...
here i tell you how i've implemented networking in box2d (well really I'm using Jbox2d, but this is meaningless) only for BODY and for a REAL TIME GAME (well the truth is it is a turn game with turn duration < 20ms. Why max 20ms? because it mean 50~60 fps!) using TCP.
First of all create an Object2d, witch contains a body and an unique ID. This object has 2 fundamental function: getInfoPosition, witch return an object containing: position x and y, angle, linear velocity and angularvelocity,
This object will be send to the client ONLY one time for object, for basic information regard the creation: so include here the ID and the "shape" or model name. i send Object2d 1 time at second just for debug the client physic engine, if the object aren't 100% synchronize it will close the client and print an error (actually more than 1 hour of test with 10 sphere, and then i turn off computer 'couse was late 🙂 ).
Now create an abstract object (let's call it "Action") with an abstract function "run (Objec2d objToModify)", and extend it for every action you want. For example now I'm using only a "ActionEngine" wich aplly an impulse and a torque to the ID Object2d. A good idea if the client generate action is to apply it to the client's asynchronous world PING/X turn after (X is the duration of a round)
Now create a NewTurn object: it will contain all the Object2ds, Actions and the number of the actual step of server, and will be send to client.
Server will send NewTurn only if it is necessary: only if an object2d or a client has generated an action in this turn/step.

Now let's create the client. It will have 2 physic world: one synchronized with the data from the server, and one that is a copy of the first+asynchronous step. What are the asynchronous step? simply, if we know our server do a step every Xms, and will not always send and NewTurn, we will synchronize the first world at the arrive of NewTurn, and the second is a copy of the first + a step for every Xms without a NewTurn. Remember to clear (or overlapping obj will create instability), regenerate body(pay attention to create also a copy of Object2d) and steps (applying client's action) the second world when a NewTurn arrive. If a NewTurn is just arrived you will probably step your asynchronous world of PING/X turn. 🙂 Now copy the position, angle, ID and model name of the Objcet2ds of the asynchronous world and send them to your graphics engine, and enjoy it^^

As you can see the absolute position and angle are send only one time at the client, but it will stay synchronized at 100% due to action. Action are very much less expansive than send all maps every turn or send the action AND the position, and will be easy limit and control the actions every turn. But is important you use TCP because lose of a NewTurn or executing a NewTurn before another will change very much. This method use about 10% of cpu (server+client without graphics in java) on a 800mhz EEEPC, when I will put my code on a SVN like sourceforge or googlecode I will put here a code snippet of the server and client.
Hope you understand my bad English and will enjoy this guide. I think for joint you will only use something similar to Object2d but i've never used them... Any tips and correction is appreciated.
Kiss Kiss 😀
****** here you can find the working network engine code, with a basic graphics (in 3d). Enjoy it^^

Sorry found and solved a weird error, here the link for working test: http://code.google.com/p/jspace2d/source/browse/#svn/prova%20sincronizzazione
Looks interesting.
I presume when the NewTurn command signals a client to clear and recreate a world, the server does the same.

How do you deal with user interaction, and latency?
nono, you can see the "NewTurn" as "NewStep", it contains the step number of the server and the action/new object in that step. The client update it's synchronous world with that informations, clear the asynchronous, copy the synchronous in the asynchronous, step the asynchronous until the client's relative step. So you can send a NewTurn only when there are new object or new action, and also lag will be less evident, i hope 🙂 .

For the user iteration: simply every iteration is an action. (well I have to code this actually, but this is the basic idea)
➡️ Server: receive the action, test if is valid, execute it and send him to all clients (inside a NewTurn)
➡️ Client who performed the action: Until it's action isn't notified by the newTurn, it will use the action in the asynchronous world, but not immediately, it will wait PING/STEPDURATION step
➡️ Clients receiver: well the newTurn they will receive is old, but is old only PING_TO_SERVER, so probably < 150ms/200ms( I hope, i'm not a magician 😆 ) . Yeah maybe before sent objects positions to GUI smooth them with the previous positions and everything will be all right

love & peace
I don't see how your synchronous world in the client and the server are identical. Two worlds with different histories, but the same externally editable properties, are not guaranteed to play identically.
well, the world has to have identical surrounding box (I've got some weird error when they get out of the AABB but it was a young version). When the client is created it will get ALL the body(with build information, I use shared classes between server and client with a function createBody(...) who will build the shape to be sure they are the same), the position, rotation, linear and angular acceleration (XFORM rules). So the world will be the same, if there aren't overlap.. this is a critical point (I've never think about this ummm 🤓)
Then you have 2 identical world, at current step, and if the external modification (Actions) are equal for the two world, applied at same step, you have identical world, as it seems 😁
Probably java is adjusting the float precision and calculation because this code works for fedora amd server and windows intel client (my machine is 64bit but still using 32bit SO, the client is a my friend machine with ping ~70 and 32bit cpu)..
As it seems box2d (or at lest Jbox2d) as proved to be deterministic (as I've read somewhere in another post, your response was box2d is lightly deterministic, but at least with body it's working well)
lestofante wrote:
Then you have 2 identical world, at current step, and if the external modification (Actions) are equal for the two world, applied at same step, you have identical world, as it seems)


Unfortunately, it's never that simple. Different cpus will calculate physics slightly differently even at the same step.
Oz1571 wrote:
Unfortunately, it's never that simple. Different cpus will calculate physics slightly differently even at the same step.


Well that why i have written this guide: I've tested it (with different architectures, see above) and everything is going all right... like you I was sure to have to do some synchronization after some step but this isn't necessary.. Maybe is java (someone is going to do something similar in other languages?), maybe the float calculation are done in double precision and then converted to float, maybe a galactic coincidence... but the code actually work and is stable! Only the case of the creation of overlapping body can break the code, but this can be easily workarounded (just control if the place where you want create the shape is free)
kiss
I hope "maybe it works" is a good enough level for your production code.
I've done some test under heavy load (100+ object moving random) and i'm getting some difference error with colliding object. This test are run with client/server on the same machine (so same math), maybe an error in the code or different math can be produced from the hardware (error is in the order of 5.313226E-8)?

"maybe it works" is not enough for me, but "it works with little error, so every 2 or 3 sec you have to resynchronize the world" it's quite good for me and many applications.. As i had say I was impressed by the the precision in different cpus (no error with 10 body) 😁

edit: after some test under heavy load seem that only some colliding object are wrong, like if shape A hits B, and produce an error, error A will be = - error B, so it seems something engine related.
okok, box2d it's not deterministic, but.. it would be amazing 😁 maybe i will port this code in C so I can test the behaviour of the latest version of box2d

Original thread: https://box2d.org/forum/viewtopic.php?t=4614