Networked Physics

ewjordan wrote:
Are there any decent solutions to this, short of actually buying a game server and a proper unrestricted connection?


A linux root server is around 20€ / month. A VPS (that's when multiple OS are running on one machine) is even cheaper: 10€ / month. The price obviously depends on the performance you want, but I had never problems with my 10€ VPS from hosteurope. I think it's affordable 🙂

We're still working on network-capabilities for Phys2D. Should it get finished, a port from Java to C++ should not be that hard, because Phys2D is based on Box2D.
I will use Sun's Darkstar Server, open source for MMOs. It's java based.
Ah, excellent then - it appears that prices have come down significantly since I last looked into getting my own server (or someone was trying to rip me off back then!).

Noya, how are your efforts progressing? If you've gotten somewhere on this, any chance you could explain your general approach and offer any advice? Phys2d is based on the Box2d Lite code, so a direct port would not be possible (also I'm certain that networking is completely different in Java and C++) but the fundamental problem is likely to be almost exactly the same, so any guidance you could offer would be great.
ewjordan wrote:
Noya, how are your efforts progressing? If you've gotten somewhere on this, any chance you could explain your general approach and offer any advice? Phys2d is based on the Box2d Lite code, so a direct port would not be possible (also I'm certain that networking is completely different in Java and C++) but the fundamental problem is likely to be almost exactly the same, so any guidance you could offer would be great.


Unfortunately I'm very busy at the moment. But since I'm very interested in this topic, I will take my time for this project the next couple of months, starting next week. I'll post a link to the design approach here. And I'm allways happy to discuss and exchange experiences 🙂

Java network code is quite similar to C (unix / windows) sockets. Since phys2d is going it's own way, it will not be a direct port, but nevertheless I hope it won't be too hard. I'd be glad to help and give advice here.
I'm having some success as I have a room with avatars that do not collide with each other only the environment. I'm simply sending the pulses.
Is there a lot of interest in a networked Box2D simulation? Over the past few days, I've put together something that solves this problem (although rather inefficiently); it is a layer that sits entirely on top of Box2D, and requires no changes to Box2D itself other than the world and object serialization stuff that I have added to my copy.

I will probably, at some point, try to integrate some more of the networking/prediction/correction model into Box2D itself for performance reasons. Is this something that other people could get mileage out of? I'd probably be willing to see if I couldn't generate some reasonably-clean patches for the current SVN version if people are interested.

What I have definitely isn't clean or well-tested enough to share quite yet, though.
That's great that you got something working, I'd love to see a demo when you feel comfortable enough to show it off! I recall Erin suggesting that embedding the networking code in the release version of the engine itself might not be the best idea because of all the additional application specific handling that needs to happen, but I think a patch would be great to have as a jumping off point. Anyone doing this type of game is going to be hacking around a lot anyways, so a little work on their part is to be expected. 🙂

How bad are the performance issues you're running into with your added layer? Are they problems with respect to local computation or network latency/bandwidth? Also, are you working out a peer-to-peer or server based approach?

Long story short, yup, I'm interested!
ewjordan wrote:
That's great that you got something working, I'd love to see a demo when you feel comfortable enough to show it off! I recall Erin suggesting that embedding the networking code in the release version of the engine itself might not be the best idea because of all the additional application specific handling that needs to happen, but I think a patch would be great to have as a jumping off point. Anyone doing this type of game is going to be hacking around a lot anyways, so a little work on their part is to be expected. 🙂

Actually, I've managed to move the world serialization stuff entirely out of the existing Box2D code and into two new classses (b2ClientWorld and b2ServerWorld) and the dozen or so classes that they use internally. The biggest issue with portability is that developing a networked simulation requires that you stick to a particular model pretty tightly. For example, you have to provide a callback to resolve collisions, and the callback must be deterministic. (This is a restriction of any client-side prediction algorithm.) I also haven't worked with joints yet--it's just rigid bodies right now--and bodies, including their attached shapes, may not be modified once created. Both of these limitations could be relatively easily overcome with more work.

ewjordan wrote:
How bad are the performance issues you're running into with your added layer? Are they problems with respect to local computation or network latency/bandwidth? Also, are you working out a peer-to-peer or server based approach?

I haven't done any profiling. I'd guess the difference is not likely to get better than about an order of magnitude, just because of how client-side prediction works.

I chose a client-server model. Peer-to-peer distributed simulation is an entirely different topic, unfortunately, and becomes much more complex very quickly. Whose temporal viewpoint is correct, when latency makes conflicting things happen? Whose viewpoint is correct if one peer decides to tamper with the simulation to cheat?

The performance issues are due to the fact that I am using full checkpointing. I keep a history of some fixed length, and I store all dynamic properties of every object at each timestep. (I am sure that, by using some sort of preallocation scheme like Box2D proper does, I can significantly improve the performance of this stage.) What really kills is that when an update-in-the-past arrives from the server, the simulation must be rolled back and resimulated, often by multiple timesteps, as quickly as possible.

This is pretty much just how client-side prediction works. However, by either using more complex external data structures or by modifying Box2D internally a little bit, it should be possible to roll back the simulation and then move forward again, only resimulating "dirty" bodies. I think that I have figured out how to do this without modifying Box2D itself (but accessing all sorts of internals). I am trying to get a nice testbed/simulator and demo for the inefficient scheme going before I try this.
I have to agree with ewjordan, this sounds awesome. Deterministic callbacks are not really a restriction at all.
Instead of modifying Box2D internally, you could track which "islands" are dirty and only resimulate those instead of the entire world.

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