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 😀
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 😀