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

Resizing a tiledmap when using phaser

1  Asked on December 16, 2020 by sora


Prefab spawning issue

2  Asked on December 15, 2020 by usmaan-mahmood


Pause updating but keep rendering

1  Asked on December 5, 2020 by yosoku-denkai


How to make it so my cursor is not behind the UI?

0  Asked on December 3, 2020 by jacksonba90


Can’t submit facebook app for review

2  Asked on November 20, 2020 by moria


OpenGL Strange mesh when animating Assimp

1  Asked on October 26, 2020 by david-neves


2D Sidescroller Gravity

1  Asked on October 22, 2020 by pete-mavrelis


Can I “unmerge” merged meshes UE4?

0  Asked on September 22, 2020 by formatc2013


unity : How to delete unused assets?

0  Asked on September 6, 2020 by andio


Ask a Question

Get help from others!

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