Multiplayer/loading/saving support

Hello,

For the future it would be handy if multiplayer support was present in box 2d.

I can imagine the following scenerio for a p2p multiplayer game with for example 6 players, perhaps a 7th joining or so.

All players/computers are synchronized and have the same state.

The 7th computer needs to get the entire state from the box 2d engine, this includes data structures as well like which objects to create and how to link them etc.

Also in practice computers/processors might execute floating point a bit differently, how much of a problem this in practice is I don't know... an idea could be to calculate a hash on the entire state each step and transmit and compare the hashes... if they are all the same it's good, if not then the game is "out of sync".

The least ammount of computer(s) with out of sync data are re-synced with data from the majority... if it's even then the group with the computer with the lowest ip address "wins" and syncs the other ones.

So what's needed for such a feature is the following:

1. Box2D needs to be able to save it's entire state to a "stream" this could also be a big chunk of memory.
2. Box2D needs to be able to load it's entire state from a "stream" this could also be a big chuck of memory.

So it seems box2d needs some binary format which can also encode it's "types/classes" like some runtime information.

I guess TypeOf(vSomeObject) could be used... (I am not sure if c++ has a TypeOf).

In case no type information is available then hard coded types/enums/constants could be used a well...

Also perhaps an identifier to identify all objects/instances.

The binary format could be something like:

packed structure
mType
mIdentifier
mObjectData
end

And so forth...

I hope this feature is included in some next version of Box2D so I don't have to modify the source code all the time 😉

Bye,
Skybuck 🙂
Did you read Gaffer's articles on networked physics?
http://gafferongames.com/

The hashes you suggest will likely always disagree. So it is better to start with the assumption that the clients will always be somewhat out of sync.

Also, you should stop asking for huge new Box2D features. See: http://en.wikipedia.org/wiki/Feature_creep.
States should be in sync.

First there is one player, second player joins, gets the entire "world/game state" and so forth.

A seperation should be made between "world/game state/information" and "player actions" (which he seems to call corrections).

Only "world/game state" and "player actions" have to be transmitted.

Each side simulates the entire world and applies all player actions to it (in a synchronized way which should be possible thanks to the stepping of box2d.).

The game will then probably only be playable for people on same continent or perhaps even farther... maybe some lag/buffering/queuing might also help but is secondair.

For now a simply "save state" / "load state" kinda of functionality for box 2d would be nice for the future.

If box2d can save/load itself from disk then surely it can be transferred over the network as well.

Could be interesting for single player games as well... the player could save the game at any moment in time and continue at any moment ! 😉 🙂
I'm not sure it make sense to send the state of the physic world from the server to the cilents :
- It's seems a really big waste of bandwidth
- The clients would be late compared to the server (usualy some prediction is used)
- Objects would teleport when loading the state (usualy an interpolation is used, but I guess you could still do this in the rendering layer)

I agree it's not the job of Box2D.
Also I would not call it feature creep.

I would call it a basic feature/capability.

Who's talking about protocols and interpolation ? I am talking about something very basic, "packing/unpacking" the world state so it becomes suitable for storage or transfer.

Here is a tip how it can be implemented in each class/layer that needs to be stored/loaded, all get the following two methods:

LoadFromStream( ParaStream : TStream ) : boolean;
SaveToStream( ParaStream : TStream ) : boolean;

Bye,
Skybuck.
If you made a Box2DNet library I would use it. Its enough work to be a separate library.
The idea is to integrate it into the library, and it could be used for other purposes then networking so calling it box2dnet would be weird.

The main reason, assuming it's a good idea, to be integrated into box2d is:

1. So it's kept up to date.
2. So it's available out of the box.
3. So it's maintained.
4. To save others the same work.

The questions to be answered is:
1. Who is going to implement it into Box2D and spent his time on it ? (If the original maintainer, Erin I believe, has no time for it, or no interest in maintaining it, then perhaps somebody else should be assigned to it to maintain it) I might be available for consultancy 🙂 and a little demonstration, but I am probably not going to implement the whole thing for all classes for now. Perhaps everybody could implement it for a little class or something, and distribute the ammount of work, that would be nice.

Bye,
Skybuck.
Skybuck wrote:
Who's talking about protocols [...] ?

Actualy you were talking about networking :

- "It would be handy if multiplayer support was present in box 2d."
- "for a p2p multiplayer game"
- "computer(s) with out of sync data are re-synced "
- "the computer with the lowest ip address "wins" and syncs the other ones"
- "lag/buffering/queuing"

Skybuck wrote:
I am talking about something very basic, "packing/unpacking" the world state so it becomes suitable for storage or transfer.

You were actually talking about networking and the feature you asked for to support this was saving and loading the physic state.
In my opinion it doesn't make sense to save the physic state, send it, and load it for the reasons I mentioned.
But a Load and Save feature of the physic state can make sense for other stuff.

Skybuck wrote:
Here is a tip how it can be implemented in each class/layer that needs to be stored/loaded, all get the following two methods:
LoadFromStream( ParaStream : TStream ) : boolean;
SaveToStream( ParaStream : TStream ) : boolean;

What a great tip...
noon wrote:
I'm not sure it make sense to send the state of the physic world from the server to the cilents :
- It's seems a really big waste of bandwidth
- The clients would be late compared to the server (usualy some prediction is used)
- Objects would teleport when loading the state (usualy an interpolation is used, but I guess you could still do this in the rendering layer)

In my opinion it doesn't make sense to save the physic state, send it, and load it for the reasons I mentioned.
But a Load and Save feature of the physic state can make sense for other stuff.


Your reasons are very vague and seem bs to me.

To me it seems all physics data is needed so that all clients can simulate the world in the same way.

I did mention it was a p2p game and not a client/server game like you seem to think.

Sending/receiving the entire physics state seems to be the most easiest and correct way of getting something working.

If you have more complex idea's that great for discussing that sometime, if it will work and work well is another matter 😉 🙂

At least we agree on something, a loading and saving feature would be nice.

Perhaps little optimizations are possible by leaving some unnecessary information out of it but that requires deep knownledge of the engine itself I guess 😉
Skybuck wrote:
noon wrote:
To me it seems all physics data is needed so that all clients can simulate the world in the same way. [...]
Sending/receiving the entire physics state seems to be the most easiest and correct way of getting something working.
)


When you do networking you want to minimize the bandwith. Sending the entire physic state is overkill.
In your packets you would get every bit on information necessary to describe all body states, all fixture states, all join states, ..., that are in the b2World. Actually every bit contained in the def classes plus dynamic informations (linear / angular velocity, ...). Imagine the amouth of wasted bandwith! Imagine again if your b2World has 1000 static edges shapes.

You really don't need to send that. Instead your server should send only the informations that cannot be guessed by the client, like the position / velocity of obejcts. You don't need to send the angular velocity if the client don't care about it for example. Your replication layer should manage this by picking data from any subsystem that needs to get replicated. (the physic being one of them)

Cheers

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