Unpredicatable Physics

I am creating a multiplayer game, and it seems the physics play out slightly different in both of the clients. I have verified the following:

  • The vector received by all clients is the same as the one played out on the active player. Example: Player 1 fires an object using certain vector. The exact same x and y values of the vector are used in it's local simulation, as the ones received by the other connected clients. All clients launch the object using body.SetLinearVelocity(velocityVector). This was verified with junkbytes debug console.
  • At first I saw the issue when the clients were on different devices, with different resolutions and CPU. But I was able to reproduce by running multiple instances of the game on my PC.
  • Every frame I was modifying the bodies velocity (rudimentary top down physics), but disabling this still leads the same issue..
  • Simulation seems to play out identical (body locations trace out the same on all clients) as long as there aren't any collisions.

The world is setup as follows (with GRAVITY = 0):

Code: Select all

var gravity:b2Vec2 = new b2Vec2(0, GRAVITY);
PhysiVals.world = new b2World(gravity, true);

The sim step happens every frame (FRAME_RATE = 30):

Code: Select all

PhysiVals.world.Step(1 / FRAME_RATE, 6, 4);
PhysiVals.world.ClearForces();


I have verified my contact listener is not modifying any of the objects on contact. If anybody could help that would be greatly appreciated! Thank you in advance, and please let me know if you need any additional information.

Although things still get out of sync when this is disabled, here is my top down friction code for anybody who is interested, feel free to use it if you like.

Code: Select all

private function applyTopDownFriction():void
{
    var vel:b2Vec2 = _body.GetLinearVelocity().Copy();
   
    if (vel.Length() > 0.0)
    {
        if (vel.Length() < 0.1)
        {
            vel.Set(0, 0);
            _body.SetAwake(false);
        } else
        {
            vel.Multiply(_combinedFriction);
        }
        _body.SetLinearVelocity(vel);
    } else {
        _body.SetAwake(false);
    }
}
I'm assuming that FRAME_RATE is a constant, not a value taken from the Stage.

The simulation is only predictable and deterministic if everything is the same frame-by-frame since the start of the simulation. If you apply the vector at different times since the start of the simulation (as in, you apply the vector 30 frames after the start the simulation versus applying the vector 60 frames after the start), the results can be different. So, for the simulations to be the same (on at least the same computer), the vector has to be applied at the same exact time since the world was created.

If you have that covered (the vector is applied at the same time since the world was created), then I'm not sure. There might be other things at work, like how the simulation is updated (is the physics updated only once per frame or potentially multiple times per frame)?

It is known that exact same simulations can be different between different computers, which is due to the differences in floating point calculations in different processors (notably, differences between Intel and AMD processors). This unfortunately an unfixable issue, since it's not really a controllable thing (and we can't switch floating points with fixed points). One workaround is to just have different sync points.
Thanks a lot for the response! FRAME_RATE is constant. Unfortunately the application of the vector does not happen at the exact same time since world creation. Since the worlds were created at different times. Even if I could solve this, I then wouldn't be able to account for any differences in CPU.

As far as syncing goes, would it be sufficient to just make sure all the actors are in the same position and rotation in the world before the players turn? I am making that assumption currently. Except for the player pieces, since I verified they start off at the same location. I will verify ALL actors are in the same location. Thanks.

UPDATE
Even when 2 objects start off at the exact same location (body position in the simulation), if there is any collision, the end result differs. I am going to do an experiment and have the active client send the sim step to the server. The receiving client will then "restart" the simulation, and apply the vector on the same step. This should let me know if that is indeed the case. Of course this would not be feasible in production environment, since the longer the sim runs, the longer it would take to restart. However, this can at least let me know if this is the cause. I will keep this post updated.
I am facing the same issue, did anyone knows how to deal with it.

I run multiplayer game on 2 devices, and after some time of bodies colliding each other, they starts to be asynchronous. Sometimes it happen after cca 20 seconds, sometimes even later, there is no rule.

Devices are getting the same vectors.

Anyone had same issue?

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