Networked Physics

An island based approach could actually work pretty well - instead of doing full checkpoints periodically, you could stagger island checkpointing to hopefully spread out the rollback simulation load on the client side. The problem would be that the islands might change between updates, so you'd have to figure out a clean way to handle stuff like that. Perhaps a loose islanding scheme would be worth considering, where an island is not split until it has been separated for more time than the network update frequency. The island generation is not too difficult, you can steal a lot of the code from Box2d itself. To make it loose you could allow for phantom connections that are generated every time a real contact/joint is destroyed and time out eventually.

I agree that p2p networking is a whole other mess that you probably don't want to dig into at this point.

One issue that we discussed a bit earlier re: determinism was that on different platforms you're not going to get true determinism because of a variety of factors, not the least of which is differing floating point resolution for intermediate (in-register) values. In light of this, every object will need to be checkpointed periodically, as you're doing. That doesn't necessarily mean they must all be checkpointed at once, though user-affected islands will need to be updated more often than others.
Hi,

I've been watching this thread, and it's pretty interesting to me because I've done what you propose for a java network game I made here: http://www.javagaming.org/forums/index. ... ic=18019.0

I re-wind the world when an old event is received, and re-update to the current time. The system I've got does the re-wind automatically using java reflection (a way of changing an object programmatically). I actually tried porting JBox2D to use the network code I made, but had some trouble with arrays and Enum's which my code doesn't work with (yet).

About your way of doing it, won't a person's game code need to be re-wound as well? Because not all state-info will be inside the box2D world, it is likely to be in the programmer's own code which will also need re-winding? I'm curious as to how you'll deal with that.

Anyway, I found that the re-updating after a rewind is actually pretty fast, so I think your approach of optimising later is a good idea. After all, for most games 90% of the CPU time is spent in graphics anyway, no the logic/physics.
CommanderKeith: Would it help the enum issue if I uploaded a Retroweaved copy of the JBox2d jar? I assume enums must be handled slightly differently under reflection and that's what's holding you up. Retroweaver rolls classes back to Java 1.4, which means that enums are wrapped into normal classes via some sort of dirty black magic. I'm not sure if that would help your purposes, in particular I'm not entirely sure how the enums end up functioning from the outside (I usually just use Retroweaver on my final result), but if you'd like to try I can send it over.

Also, if I was to start poking around the code you posted at JavaGaming, where would I find most of the stuff dealing with reflection? I'm curious to see how you handled this.
The short answer is "yes"--and the best way to handle this in a generic fashion is actually something I'm thinking about right now.

Right now, the best way I have thought of is this: When the server decides that it needs to replicate a body to a client (as decided by a callback that you provide on the server side), it uses a callback that asks you to "pack up" the entity represented by the body. You provide some sequence of bytes, and a matching callback is invoked on the client, saying "here is a new entity that has just arrived." The server assigns a unique network ID to each rigid body, which in many cases will probably be a good enough way of distinguishing between unique game entities. In my project, for example, I envision using this system to provide information about how the entity corresponding to the rigid body should be rendered, what sort of gameplay object it is, etc. Keep in mind that your user data pointer will not be transported across the network automatically, so using this pair of callbacks is also a nice way to serialize and deserialize whatever user data you attach to each body.

(To handle changing entity properties, I have a simple mechanism in place that allows the server to send messages to a specific entity on a client. An easier-to-use or higher-performance attribute replication system is probably in the works some time down the road.)

To handle unwinding and rewinding, the event callbacks ("contact", etc) have a boolean argument which indicates whether or not this is an initial simulation or a re-simulation. In a re-simulation, only side effects of the event that affect the simulation should be handled. For example, if you play a sound effect, or give the user ammunition, or something along those lines, you should only do that in initial simulation.

The nasty little detail, of course, is how to handle the situation where an event /doesn't/ happen in resimulation. Imagine that you shoot a missile and it hits an enemy, causing an explosion. However, when some delayed updates arrive, it turns out that the enemy moved out of your missile's way, and no collision took place. What's the right way to handle this? I suppose it's relatively rare, and we /could/ always just write that off as inevitable de-synchronization, to be corrected by periodic state updates, but it seems like there should be a relatively nice solution to the problem.

I'll dig through my little library and see if I can come up with anything. Any ideas you guys have would be most welcome.
I like your enthusiasm ewjordan! Thanks so much for offering to retro-weave the jar, but the problem is not with the class files, it's with my own code. The classes that do the rewinding (and serialisation) can't deal with enums basically because they're special (they can't be created using 'new', they're a singleton, and can't be re-created or over-written) and I haven't figured out how to do it efficiently. But it can be done, and I'm feeling inspired thanks to you so I'll have a go in the next few days.

As for arrays, they can be dealt with easily, they're just a little more troublesome compared to using an ArrayList (which I extended to make a rewindable and serializable version called ArrayListSS). If you're interested in the code, have a look at the sydneyengine.superserializable.SSAdapter.makeEqualTo method, that's the one that does the automatic re-wind. It's called by the ain game world object sydneyengine.RewindableWorld's makeEqualTo method which does the re-wind. All of the code in the sydneyengine.superserializable package deals with custom serialization (custom because existing objects are updated instead of re-created when they are deserialized) and re-winding (thru the SSObject.makeEqualTo(SSObject) method).

I'll have another crack at integrating enums (failing that, there's always the possibility of just replacing them with static final constants, but I think they work well in JBox2D the way you and quixotel have done it, so I'll persevere 🙂 )

Oberstille: I like your ideas, we should keep each other informed of progress. The problem with my code/engine/framework is that it kind of completely envelopes a developer's game code - all of his game objects have to implement my SSObject interface. The problem with yours, I'm guessing, is that non-Phys2D data will be hard to re-wind and probably messy for the developer to understand (not that my approach is understandable either!). Whatever method looks like the best, we should each other informed - I'll post up my source if/as soon as I get something working.

About the explosion that was actually dodged: my framework just rewinds the world to where the player dodged, makes him dodge, then advances time forward to the present, so there's now no explosion. In other words, for the client that fired the missile which intitially hit the enemy player and exploded, when the dodge event was recieved the client now sees the enemy teleport to the left, the explosion disappears and the missile re-appears and continues on its merry way.
Re: ArrayList vs. arrays, quixote_arg and I have been talking for a while about whether or not to migrate to Java's container classes. It would probably be easier for everyone (especially ourselves!) if we moved everything over, but I'm still quite concerned about speed. The last time I ran tests, every one of Java's container classes was slower by at least a factor of 4 than a flat array, and the difference sometimes got worse when you were just storing built in types. I haven't tested the version 6 JVM yet, though, so we might switch over if the performance doesn't suffer too much. Definitely not before 2.0, though, which I'm just starting to take a look at to port now.

I'm interested a lot in what you're doing because I've been hoping to do something similar for a long time, but I've never had the time to learn the networking-specific end of it. I'm curious, will your methods work in unsigned applets, or are there security issues you need to dodge?

Good luck to both of you, in any case!
How's it coming for all of you?

I have been very busy with coursework, but I think I've ironed out the last of the issues in the basic system. There are a few more things that need to be added before the library will be useful to anyone, myself included: bodies can't be created or destroyed during the simulation yet, and most of the hooks for application-specific behavior (the deterministic callbacks I discussed earlier) don't actually exist yet.

I'm working on a testbed application that will render multiple worlds at the same time (e.g., so that you can see what the server and a few clients see at the same point in time), and will let me play with objects and network settings at will so that I can stress-test the library.

I've got it pretty much entirely untangled from Box2D at this point. It does depend on being able to get/set some internal variables, so it's not amazingly cleanly separated, but it doesn't involve any source changes any more.

I'm eager to hear how all of your work is coming!
Nice work, sounds cool.

My progress isn't going so great, I'm painting my apartment and then moving out so I haven't had a chance to do anything 😳
I just want to let you know that Glenn Fiedler (Gaffer) released slides of his GDC talk on networked havok physics in Mercenaries2. I haven't read it yet, but maybe it will help you:)
Summary of the talk: http://xemu.blogharbor.com/blog/_archiv ... 36354.html
Slides: http://www.gaffer.org/archives/gdc-slid ... ed#more-83
Yeah, I went to that talk, the gist of it was that a) rollback and resimulate methods are too expensive, and b) if you can trust the client to properly report his own simulation state, then you can use that to avoid the problem of a), and that's what they're doing in Mercenaries 2. For competitive games this could be a problem, so you have to try and address cheating in another way - Mercenaries 2 is coop, so it wasn't as big a deal.

Could be worth trying with Box2d - for smaller games, you might not run as high a risk of having cheaters hack your client, so you could get away with trusting the client a bit more.

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