2D Networked Physics Simulation

Started by
4 comments, last by Inferiarum 11 years, 7 months ago
I've put together a simple simulation of a chess board with 2D physics. Really simple, just top-down colliding squares. Right now all of the simulation is on the server. This actually works surprisingly well with little chop, but of course there is significant latency. I want to also run the simulation on each client, but the more I think about it the more complicated this gets.

1. The position of each piece needs to eventually end up synced.
2. If a piece is being moved by a player, it can collide with a piece, which can collide with another piece which can collide with a fourth piece which is being moved by another player.

One common solution is to only send manipulation events to the server. Get the changes made to a piece by direct interaction from the player and send positional information about it. The problem is that if I let the server just apply those changes then the likelihood that other pieces which were collided during the course of the manipulated pieces movement would closely sync with the client simulation is very low. This would break #1 above.

So let's say I can get a list of all "awake" bodies. In this case that would be any pieces that are being affected directly or indirectly from input by the player. I would then send updates for each of those bodies to the server. The problem with this arises when two players are both indirectly affecting the same piece, as in #2 above. It's possible to receive two distinctly different updates for one piece. Maybe I could average the updates together in some way? But this would lose accuracy and I feel like there would be a lot of fighting between the client and server simulations.

The ideal is to have client prediction and have a configurable small amount of latency. I just can't see how it could be accomplished in this case. Is anybody else working on this? Suggestions? Is this impossible to do well?

Thanks.
Advertisement
Its very, very hard to get right.

Some tips:

* use a fixed time-step on client and server
* send player inputs and correct for latency - keys must be down for the *exact* number of time-steps on both client and server

The goal is to try and get exactly the same results on client + server given the same inputs. You'll probably need to hash the state of the client and compare that against the hashed state of the server to detect desyncroisation issues.

Cheers, Paul.
Thanks. Is detecting desync ever accomplished using some kind of locality-sensitive hashing? My concern is that if I'm using a physics engine like box2d then there's no guarantee that the two simulations will have strongly similar results even given the exact same beginning state and inputs. The more I think about this problem the more incredibly complicated it becomes.

Also, what the hell happened to gamedev.net? :o
Look up client prediction algorithms (Gaffer Networked Physics).

The aim is to remove the latency you would get with a fully authoritative server.

i.e. client sends inputs to server, server processes inputs, run physics, returns the result back to the client. That means a round trip latency for everything the client does. In some cases that's acceptable. In many cases, it's not.

So there is a predcition / correction algorithm running between the server and clients that allows the clients to affect their view of the world in real time, while the server verifies client calculations and applies corrections for unforseen events (i.e. when two players are interacting with each other, which cannot be accurately predicted by any of them).

EDIT : That's the 5th time I've answered to that question in a month. :) Seems to come up quite a bit lately.

Everything is better with Metal.


Thanks. Is detecting desync ever accomplished using some kind of locality-sensitive hashing? My concern is that if I'm using a physics engine like box2d then there's no guarantee that the two simulations will have strongly similar results even given the exact same beginning state and inputs. The more I think about this problem the more incredibly complicated it becomes.

Also, what the hell happened to gamedev.net? ohmy.png


Yes, this problem is hideous. You may have to author your own engine to get the guarantees you need.about the determinism.

Alternatively you can use a non deterministic engine and send correction vectors along with the keyboard inputs.

Look up client prediction algorithms (Gaffer Networked Physics).

The aim is to remove the latency you would get with a fully authoritative server.

i.e. client sends inputs to server, server processes inputs, run physics, returns the result back to the client. That means a round trip latency for everything the client does. In some cases that's acceptable. In many cases, it's not.

So there is a predcition / correction algorithm running between the server and clients that allows the clients to affect their view of the world in real time, while the server verifies client calculations and applies corrections for unforseen events (i.e. when two players are interacting with each other, which cannot be accurately predicted by any of them).

EDIT : That's the 5th time I've answered to that question in a month. smile.png Seems to come up quite a bit lately.


I think this prediction stuff is meant for games with a small amount of physical interaction between players, like the typical first person shooter. Additionally, in first person shooters even very small latencies degrade the gameplay so it is a good idea to integrate something like this.

I do not really know the intended gameplay, but e.g., in the lock step approach used in many RTS games, they usually just live with the latency and try to hide it somehow (acknowledgement animations etc).

This topic is closed to new replies.

Advertisement