Networked Physics

I know getting responsive real time networked physics working is a huge effort (where by "responsive" I mean that you see the results of your actions before your opponents have necessarily received the packets generated by them, so that the opponents end up needing to "fix" their simulations once they do receive word), and very dependent on the particular game, which is why this is not in the feature request section.

I'd like to start a conversation about this, though, because I'm curious as to what would be involved. Is it possible with Box2D in its current state, or would the internal changes be enough so that it would really need to be a spinoff project?

Something like time-warp would seem to be appropriate for network physics, but that would require a lot of internal changes to the engine.
The server could do all the simulation and send out snapshots of the simulation world. Clients continue the simulation until they get a new snapshot. Any input received after the timecode of the new update will have to be re-simulated, so all input will have to be saved. I don't think it would be too difficult to get something relatively smooth going. Anything not directly influenced by player input should simulate smoothly, but there might be jitter with player input. It all depends on how quickly you can get updates from the server, which means reducing latency and the size of the updates.
So the trick is probably in setting up a lean correction format that the server would timestamp and send out (probably partially a game-specific task), as well as getting the simulator to store the past second or two of state information for rollbacks (could be done in physics engine itself). I suppose on the client side you'd also probably want to avoid rolling back the entire simulation, and only re-simulate the affected objects.

Roughly speaking then, if we chose a light snapshot route (as opposed to heavy snapshots, which would completely specify the state of the world) we'd have something like this:

Client-side
1) If a snapshot has arrived from the server:
- Check if a later snapshot has already arrived - handle in some way if it has (could be tricky).
- Roll back 'dirty' objects to snapshot time and alter states as necessary
- Compute current 'real' time based on server timestamp and measured average latency
- Simulate dirty objects up to real time
- Either roll back other objects to 'real' time or simulate them forward - should be within a frame or two already, hopefully
- Die/get kicked from server if you're too far out of whack, either due to latency, cheating, or slow computation
- Maybe: every once in a while, compute a world hash and compare to server's, ask for a full snapshot if they differ
2) Client performs simulation step.
- If change in player state has occurred, send timestamped message to server.

Server-side
1) For each player:
- check for new messages, add to queue
2) Sort message queue by timestamp.
3) For each message in queue:
- Sanity check - see if timestamp is reasonable, i.e. does it match up with known latency/frame-rate, or should a warning/kick/ban be sent?
- Roll back to message time.
- Simulate up to next message time.
4) Simulate up to real time.
5) Broadcast timestamped snapshot.
6) Carry out normal time step.

Does that sound about right? The rolling back of the game states would probably require a lot of engine modification, but apart from that it seems pretty straightforward.
Hi Guys,

I've been working on a something similar for quite a while now. And I want to integrate JBox2D into it, I was recently asking about what would be the best way to do it: http://www.javagaming.org/forums/index. ... ic=17796.0

Those steps you outlined ewjordan is what I've set out to achieve with my little project. The challenge has generally been threading problems and automatic serialization.

I'll post up a very simple demo I've been working on by the end of the week. It's just a simple thing where a black dot follows the mouse cursor, but it works on a LAN with lots of players with their own dots.

Cheers,
Keith
You may find this helpful:

http://www.gaffer.org/game-physics/networked-physics/
Here's that demo (requires java 1.5 or better):
http://www.freewebs.com/commanderkeith/ ... eDemo.jnlp

After clicking 'create new game' you can click the link above again to fire up another window, and click 'join someone else's game'. Since the default IP is your own computer you can then have a two-player game with yourself. When you wave the mouse around in one of the windows you should see the player dot move towards it on both windows, but with a lag of 0.5 seconds (which is simulated). If you make more clients, you can see that mouse moves on a client are first sent to the server, and then to the other clients.

It works over LAN but I haven't tried it over the internet. Here's the source if you're interested:
http://www.freewebs.com/commanderkeith/ ... ne/src.zip

memoni wrote:

Wow, so many comments! Thanks for the link.

If anyone would like to know, I can try to explain the way the code works. My next goal (now that this simple demo is hopefully stable) is to use it with JBox2D to make interesting network games.
@CommanderKeith - interesting work, I looked at your post over on JavaGaming. If you're having trouble integrating with our code and there are any changes in JBox2D that might make this easier, let me know, I would definitely be willing to make some edits in the code (if, for instance, you need ongoing public/protected access to certain fields or methods, that would be simple - Box2D leaves almost everything public, but in the spirit of Java we've been trying to make things private unless it becomes apparent that they need to be accessible). Even going as far as adding a couple of methods or stubs per class would be fine as long as the existing methods didn't need to be changed too much - every logical difference from the C++ version makes updating that much harder, so I'd like to avoid these if possible.

Basically, I'd love to help in any way possible, as I'm very interested in seeing this working and possibly even incorporating it with the project, though I don't think I or quixote_arg have enough time to act as maintainers of the networking code ourselves. If you wanted to take that on, I'm sure it would not be a big deal at all to add you to the project and give you SVN write - talk to quixote_arg about that if you're interested, he's the project admin.

It sounds like this would probably also solve the state loading/saving issues under discussion elsewhere (it would have to, I imagine, to handle the rollbacks), at least in Java. I'm curious, could your code possibly work to connect to a server from an applet, or would applet sandboxing prevent that?

Edit: started writing this before your post with the demo, just got to try it out now. Looks good, even with the high simulated lag. I'll take a look through the code later, but this is definitely a very good start.

@memoni - thanks for the link, I remember seeing that explanation somewhere before but couldn't remember where. Looking it over, I think that sums up everything I was groping at before. The devil's in the details of course, and it sounds like CommanderKeith has this pretty far under construction, so I'm very curious to see how it all works out.
Hi ewjordan,

> I'm curious, could your code possibly work to connect to a server from an applet, or would applet sandboxing prevent that?

From memory I'm pretty sure that if the server is the same computer as where the applet code was downloaded from then the applet can work in the sand-box. I think this is how Runescape works.

Thanks heaps for the offer of your help! Actually one way that you could really assist is by making a zip of the source code in JBox2D (like you did here, but it's out of date: http://www.ewjordan.com/jbox2d/Pendulum_src.zip).

Cheers,
Keith
Absolutely, as soon as I'm done with the 1.4.3 update I'll put up another .zip - I'll try to remember to do this more regularly from now on, too.
Thanks ewjordan, I'm looking forward to it.

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