Rewinding the simulation for specific instances

Hello all. I've been working on an online action platform game lately and I'm running into an issue that has me stumped.

The game uses Box2D mainly for player collisions and movement. Players can not affect each other physically. Right now there are no interactions between two dynamic objects (there will be eventually, but I have a few ideas on how to handle it). The networking system I've devised to prevent speed and teleport hacking involves making it so both the client and server can rewind specific instances and replay them to the current simulation point with new input. I do this by saving the x, y, linear velocity, and input variables of each player for a set period of time. Then, when I need to rewind, I deactivate all dynamic objects except the one I'm trying to simulate again. The rest don't need to be simulated again because they don't affect each other. The system then gets stepped through it's paces the correct number of times and in theory the object should now be in the correct position. This works great, most of the time. I'm noticed though that under certain circumstances the client and server disagree on the final position and the client's position gets snapped.

Now, it's entirely possible the bug is in my network or gameplay system somewhere. However, some people have reported that the snapping gets worse when more than one person moves, making me wonder if the issue is in how I'm rewinding Box2D. Usually, one game is running the actions in real-time like normal, while the other has to rewind to a previous position and do the simulation within a loop. Like I said, I'm saving all bodies' x, y, and linear velocity for each step. I'm not saving anything to do with angles because all the dynamic body instances so far use fixed rotation. Before I deactivate any body I save it's linear velocity to load after it's activated again, because it seems like deactivating causes bodies to lose velocity (I could be grossly mistaken, but I'm looking for anything here). Both client and server use the exact same calls to Box2D, including the ones to step the world (they actually link to the same source file). What else could be causing two otherwise identical simulations to get off sometimes? What other variables might I not be saving and reloading properly? Is there any way momentarily deactivating some bodies' while another is simulated can alter those bodies' simulations? Can simulation between PCs, even with all the same variables, settings, timings, etc, differ in their outcome? It seems like most of the time the snapping is pretty mild, but in a platform game a difference of a few pixels can be the difference between landing on a platform and not, so this is getting frustrating. Thanks for the help!
How did you get on with this? As I recall, this is one hell of a can of worms....

Are you using different hardware or platform for server and client? You might be having problems with the differences in floating point calculation on each. There will also likely be differences between debug and release build, at least with the default settings for MSVC. Remember these could be compiled into the libraries you are using too... :p
Make sure your player input is getting applied at the right step, even one step too early or late will not show up immediately to your eye, but will cause everything to stray.
Make sure you are using the correct timelength for each timestep, if you have variable timesteps.
There could be something internal to Box2d which also needs to be stored and reset to the previous values before re-simulating, other than the obvious velocity etc that you are storing, maybe Erin can comment.

If this keeps bugging you, I suggest the following to figure it out - I know this really sounds like overkill but I eventually had to do it for my online game in which the server and client each ran the simulation (using ODE) entirely from player input actions and never shared the actual worldstate at all, so it needed to be 100% deterministic. My game also featured recording of replays (once again, only the user input) which turned out to be a dependable way of locating where things went wrong.

1. Don't use random numbers anywhere (shouldn't have to say that, but just in case!)
2. At each world step, create a CRC for the world state using any data that needs to be reproduced correctly. The CRC is just a quick and simple way to check if two world states match. Store this with the replay information.
3. When running your replay, before and after each step calculate the CRC for the world you got and see if it matches - you might be surprised how early it goes off before you actually notice. I say 'before' as well, just in case something other than your physics worldstep has changed the world without you realising.
4. While a CRC difference will tell you something is wrong, it wont tell you where. When you get a replay which fails reliably, dump a textual representation of the information the CRCs are made from to a file on both client/server, and text diff it to find the divergence. *** For this, dump floating point values as their hex representation, not with printf %f or the like!! ***
5. You might also consider keeping a track of the number of physics steps since the world began, to see if each computer is really running the same inputs on the same world (maybe log the inputs used too).
6. You might also wanna set the floating point precision explicitly in your program and *** do it for every thread!! ***

Ideally you wouldn't actually be playing the game to do all this - once you get a replay that consistently differs between server/client you will want to use it some way that you can give it to the server and client to run without messing around connecting, logging in, starting a game etc. Although having said that, I didn't really do step 3 above, I just dumped the textual representation in 4 to log files on both the client and server while playing, then when I saw something going wrong (in the game itself) I looked up the logs to see where it started using the text diff. For text diffing the dump, it helps greatly to be using the exact same source files for the physics code on both client and server so that your logging code is identical.....

fwiw the hardest to track down problem I had was caused by ODE having different preprocessor flags for different platforms, as in: #ifdef LINUX do something, otherwise do something else... wtf!! We wouldn't do that here at Box2d... would we?

Also see here, it would be nice if my frustration helped someone else out:
http://66.199.249.18/diary/
...start reading from "2006-11-23" in particular check out the compiler flags for floating point options if you are using linux server + windows clients.

have fun! lol
see here too if you haven't already:
viewtopic.php?f=3&t=4614
viewtopic.php?f=3&t=4489
As Boris says, Box2D has basic determinism: same CPU, same build, same run-time.

Box2D has a lot of internal state to optimize calculations. The internal state can also effect the the result.
irresistible force wrote:If this keeps bugging you, I suggest the following to figure it out - I know this really sounds like overkill but I eventually had to do it for my online game in which the server and client each ran the simulation (using ODE) entirely from player input actions and never shared the actual worldstate at all, so it needed to be 100% deterministic. My game also featured recording of replays (once again, only the user input) which turned out to be a dependable way of locating where things went wrong.

1. Don't use random numbers anywhere (shouldn't have to say that, but just in case!)
2. At each world step, create a CRC for the world state using any data that needs to be reproduced correctly. The CRC is just a quick and simple way to check if two world states match. Store this with the replay information.
3. When running your replay, before and after each step calculate the CRC for the world you got and see if it matches - you might be surprised how early it goes off before you actually notice. I say 'before' as well, just in case something other than your physics worldstep has changed the world without you realising.
4. While a CRC difference will tell you something is wrong, it wont tell you where. When you get a replay which fails reliably, dump a textual representation of the information the CRCs are made from to a file on both client/server, and text diff it to find the divergence. *** For this, dump floating point values as their hex representation, not with printf %f or the like!! ***
5. You might also consider keeping a track of the number of physics steps since the world began, to see if each computer is really running the same inputs on the same world (maybe log the inputs used too).
6. You might also wanna set the floating point precision explicitly in your program and *** do it for every thread!! ***

Ideally you wouldn't actually be playing the game to do all this - once you get a replay that consistently differs between server/client you will want to use it some way that you can give it to the server and client to run without messing around connecting, logging in, starting a game etc. Although having said that, I didn't really do step 3 above, I just dumped the textual representation in 4 to log files on both the client and server while playing, then when I saw something going wrong (in the game itself) I looked up the logs to see where it started using the text diff. For text diffing the dump, it helps greatly to be using the exact same source files for the physics code on both client and server so that your logging code is identical.....

fwiw the hardest to track down problem I had was caused by ODE having different preprocessor flags for different platforms, as in: #ifdef LINUX do something, otherwise do something else... wtf!! We wouldn't do that here at Box2d... would we?

Also see here, it would be nice if my frustration helped someone else out:
http://66.199.249.18/diary/
...start reading from "2006-11-23" in particular check out the compiler flags for floating point options if you are using linux server + windows clients.

have fun! lol
Thanks for the very in-depth reply! I think I've gotten it working well enough fortunately. It's still not 100% smooth or perfect, but its smooth enough I think. The fact that players don't directly affect each others' physics makes things a lot easier and small differences more negligible. A small function has been added that smooths out the error if the absolute server position conflicts with the client's calculated position. Moving them those few pixels over a short length of time really makes a big difference in the perceived smoothness of the game. Of course, every once in a while those few pixels means that you've missed a ledge on one game but not the other, but there's little I can do about that it seems.

Yes, client and server use different hardware, since this is a game that's aiming for open distribution. I know there's some wiggle in this, and am OK with that. As long as its fairly close then the error smoothing should be able to handle it.

Hmm, I didn't realize that debug and release builds can differ. I think I have accidentally mixed those before. I'll keep an eye out for that.

Yeah, syncing everything to happen on the same step has been a pain, but I think I finally have it working. The only problem arises when the sync gets off in extreme cases, but in those cases some snapping is OK.

Time steps are locked. I didn't want to deal with that hassle, haha.

Yeah, hidden internal values are what I was afraid of. Looks like Erin has confirmed that there are values that may be getting off. I guess I can't do much about this though unfortunately. Hopefully the error correction will continue to work well enough for the application as I add more objects and players.

The CRC idea is interesting. If nothing else, I may implement that down the line sometime simply to save on network traffic if possible. I'll kick in those debug tips though if the current methods don't work in the long run.

Yikes, those platform differences sounds pretty mean. I'm working entirely in Windows right now, but there's a good chance the server will be ported to linux in the somewhat near future. Sounds like you may have helped me avoid some headache on that one.

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