1
\$\begingroup\$

I am trying to implement server-side movement using box2d on libgdx client and planck-js(box2d for javascript) on nodejs server. I am running box2d world on both client and server.

Before server i was moving player on client with applying force on input

player.body.applyForce( Vector2(-4.5f, 0f), player.body.worldCenter, true)

Now from client to server i am just sending input data and applying force on server side:

Client if left key pressed:

 var inputData:JSONObject = JSONObject()

            inputData.put("type", "left");
            inputData.put("x", player.body.worldCenter.x);
            inputData.put("y", player.body.worldCenter.y);
            socket.emit("inputFired",inputData)

So server gets this data and applies force code to move player on server side.

    var movePlayer = find_playerid(this.id); 
    if (!movePlayer) {
        console.log('player does not exist'); 
        return;
    }
      console.log(data)
      var positionVector = Vec2(data.x,data.y)
      if(data.type =="left"){
        var impulseVector = Vec2(-4.5,0.0)

      }else if(data.type =="right"){
        var impulseVector = Vec2(4.5,0.0)

      }else if(data.type=="up"){
        var impulseVector = Vec2(0.0,4.5)

      }else if(data.type=="down"){
        var impulseVector = Vec2(0.0,-4.5)

      }
  movePlayer.body.applyForce(impulseVector,positionVector,true)
      movePlayer.x = movePlayer.body.getPosition().x
      movePlayer.y = movePlayer.body.getPosition().y
      console.log("new position")

After this point what is confusing me is what should i really return to client ? If i just return position values of body after applying force,how im gonna position client side body according to this ?

\$\endgroup\$
0
\$\begingroup\$

With what you have already laid out you want to use snapshot-interpolation.

Read this very good article series to learn about snapshot-interpolation:

This explains in all detail how you could synchronize a networked physics simulation by sending compressed snapshots of the game state and interpolating between them.

You can also simulate locally and on the server to use the server side position to correct the locally generated position, this allows you to hide lag.

| improve this answer | |
\$\endgroup\$
-1
\$\begingroup\$

What i did is just returning new linear velocity of body to client.So server applies force then gets linearvelocity of body sends it to client and client equals new linear velocity to body but still having sync problems.asked this on another question

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.