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);
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 ?
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
2 Asked on December 12, 2020 by paytopwn
2 Asked on December 3, 2020 by zawada
0 Asked on December 3, 2020 by jacksonba90
2 Asked on November 28, 2020 by trgamer-tr
1 Asked on November 26, 2020 by kaod
0 Asked on November 14, 2020
1 Asked on November 13, 2020 by 1234567890
1 Asked on October 18, 2020 by phoenix
1 Asked on October 11, 2020 by big-t-larrity
1 Asked on October 2, 2020 by ali-nategh
0 Asked on September 26, 2020 by nairou
1 Asked on September 18, 2020 by grimelios
Get help from others!
Recent Questions
Recent Answers
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP