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 ?