Server Side Movement Box2D LibGDX

Game Development Asked by doggodonger on August 24, 2020

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

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

    var movePlayer = find_playerid(; 
    if (!movePlayer) {
        console.log('player does not exist'); 
      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.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 ?

2 Answers

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

Answered by doggodonger on August 24, 2020

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.

Answered by VaTTeRGeR on August 24, 2020

Add your own answers!

Related Questions

Compute shader: Property at kernel index is not set

1  Asked on November 2, 2021 by glen-pierce


How can I cast light onto a sprite in 3D environment?

1  Asked on November 2, 2021 by vern-rowan


How to run skactions randomly

0  Asked on November 2, 2021 by why25


AntiGravity Vehicle?

1  Asked on November 2, 2021 by roswell


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir