Box2D Returning LinearVelocity From Server To Client But End Up With Different Body Position Between Server and Client

Started by
5 comments, last by hplus0603 5 years ago

I am trying to use box2d on nodejs server with libGDX client.What i am trying to apply is authoritative server modal so clients are just sending input data to server, server is running simulation and returning data to clients.

 Client sends right key pressed input data to server,server calculates applyForce vector, applies force to server side body.

Server is sending worlds body data to clients such as linearVelocity,position angular velocity etc with `sendWorldData()` function.After geting data clients equalize this data to client side bodies.

Right now i just equalize linear velocity data,if i use setTransform on body for same position, it breaks the simulation and game crashes.I guess just linear velocity is way to go for now.
The problem is even though i return the same linearvelocity from server to client,bodies end up on slightly different x,y positions.

   


 client is
    (6.8612046,7.3647866)
    server is
    (7.260152142360204,7.807573379341183)

I read a lot about this problem and started by trying to fix timestep on server and client.
Client timestep:
 

   


 private var fixedTimestepAccumulator = 0f
        private var MAX_ACCUMULATED_TIME = 1.0f
        val TIMESTEP = 1 / 60f
      fun update(delta:Float){
            fixedTimestepAccumulator += delta;
            if(fixedTimestepAccumulator > MAX_ACCUMULATED_TIME)
                fixedTimestepAccumulator = MAX_ACCUMULATED_TIME;
    
            while (fixedTimestepAccumulator >= TIMESTEP) {
    
                world.step(TIMESTEP, 6, 2);
    
                fixedTimestepAccumulator -= TIMESTEP;
            }
    
            inputUpdate(delta)
            cameraUpdate(delta)
            tmr.setView(camera)
            batch.projectionMatrix.set(camera.combined)
    
        }


Server update method:

 


   var lastUpdate = Date.now()/1000;
    var fixedTimestepAccumulator = 0;
    var MAX_ACCUMULATED_TIME = 1;
    var TIMESTEP = 1/60;
    
    function physics_hanlder() {
    
        var now = Date.now() / 1000;
        var delta = now - lastUpdate;
        lastUpdate = now;
        fixedTimestepAccumulator = fixedTimestepAccumulator+delta;
        if(fixedTimestepAccumulator > MAX_ACCUMULATED_TIME)
            fixedTimestepAccumulator = MAX_ACCUMULATED_TIME;
    
        while (fixedTimestepAccumulator >= TIMESTEP) {
            world.step(TIMESTEP,6,2);
            sendWorldData() // sends servers body data to clients
            fixedTimestepAccumulator -= TIMESTEP;
        }
    
    
        
    }
    
    setInterval(physics_hanlder, 1000/60);

 

Advertisement

First: JavaScript is not a deterministic runtime. It may not give you exactly the same outcome across different hosts.

Second: Box2D is not a deterministic simulation library. It may not give you exactly the same outcome across different instances.

Third: That being said, the difference you show on client and server are way bigger than simple non-determinism would account for in a small amount of steps.

To debug this, I recommend you create a simple test case that "does the same thing" each time. Log the timestemp number, position, and applied velocity of the object for each time step, on client, and on server. Open the two logs side by side, and line them up where they start at the same position, and then see where they diverge. Go back, and at that step number, log all of the states (spin, velocity, possible colliders, etc) and see what's different.

enum Bool { True, False, FileNotFound };

trying  i will edit

after one input of movement there is like 0.2 position difference between but after more input difference increases

Please excuse the intrusion, but I was wondering if communication travel time could be a contributor? I missed the original post before edit, so I might be way out of line.

What does your movement code look like? Do you multiply vector by time? Is the time delta the same on client and server?

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement