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.
@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.
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.