1
\$\begingroup\$

I am trying to use Box2d on nodejs server with libGDX client. What I am trying to apply is authoritative server model 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 world's 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);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Let's say that the communication delay between the server and the client is 50ms, not really unusual, but still noticeable.

If for instance we're dealing with the player who's going upwards but suddenly starts going to the right, this information takes around 50ms to reach the server. Since that timeframe contains multiple updates probably, the player character on the server will drift a little.

This is the main reason people don't use extrapolation that much nowadays. Instead they generally go with interpolation.

Instead of knowing the current state of the game and guessing the states after it, you take the last and current state and interpolate the object between them. This introduces a couple of problems for fast paced games (e.g. in shooters you need to go back a bit to see what the players were shooting at), but this is easier to solve, than lowering latency. If you don't do some kind of calculation, you'll get the PUBG experience of having to shoot 5 feet in front of someone, to hit them.

However, if the game isn't fast paced and the exact position of the players doesn't matter that much, you could just send the positions to the clients instead of velocities.

\$\endgroup\$
3
  • \$\begingroup\$ i dont think my current problem is because of lag,i am testing this on local host ? \$\endgroup\$ Mar 21, 2019 at 22:51
  • \$\begingroup\$ @doggodonger localhost has latency too \$\endgroup\$
    – Bálint
    Mar 21, 2019 at 23:53
  • \$\begingroup\$ i really need some code examples for this topic.couldnt make this work :/ \$\endgroup\$ Mar 23, 2019 at 21:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .