Jump to content
  • Advertisement
lifesuxtr

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

Recommended Posts

Posted (edited)

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);

 

Edited by lifesuxtr

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Posted (edited)

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

Edited by lifesuxtr

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!