1 Replies - 1004 Views - Last Post: 12 April 2019 - 07:29 AM Rate Topic: -----

#1 lastpeony   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 14-October 17

Box2D Server-Client Movement Position Syncronization

Posted 20 March 2019 - 01:24 PM

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

This post has been edited by lastpeony: 20 March 2019 - 01:26 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Box2D Server-Client Movement Position Syncronization

#2 lastpeony   User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 75
  • Joined: 14-October 17

Re: Box2D Server-Client Movement Position Syncronization

Posted 12 April 2019 - 07:29 AM

just return the position and velocity at the same time guys and update it before world.step otherwise it throws error this is how i kinda solved this problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1