I have an authoritative server which is sending position data to client every 100 ms. Ping between client and server is about 70 ms.(which is actually a variable)
I have few questions regarding to interpolation between 2 server states recieved by client.
1-) Where should i place my interpolation code ? Inside my clients update method or should i interpolate whenever i recieve 2 update from server ?
Example:
fun onWorldData(data:JSONObject){ // called when update arrives from server.
//initially serverUpdate1 and serverUpdate2 are empty objects.
serverUpdate1 = serverUpdate2
serverUpdate2 = data // data is new data which is just arrived from server
if(serverUpdate1.has("x")){ // when at least 2 updates arrived interpolate and set position(just for 1st call)
var blackPieceBodyX = Interpolation.linear.apply(serverUpdate1.get("x").toString().toFloat(),serverUpdate2.get("x").toString().toFloat(),0.15f) // how to calculate 3rd parameter ?
var blackPieceBodyY = Interpolation.linear.apply(serverUpdate1.get("y").toString().toFloat(),serverUpdate2.get("y").toString().toFloat(),0.15f)
blackPiece.body.setTransform(Vector2(blackPieceBodyX,blackPieceBodyY),0f) // this code sets position of body
}
}
As you see interpolation code is called when i recieve update from server, not all the time inside update.
2-)How do i calculate 3rd parameter of Interpolation function ? In above code i was just experimenting with arbitrary numbers.Since server sends it every 100ms and ping is around 70,what should be 3rd parameter of interpolate function and how to calculate it ? Since for sure ping will be part of that calculation formula should i ping server to check ping to use in that formula all the time ? Or timestamp each serverupdate sent by server and when recieved get clients time and substract them ?
3-)Another approach i tried is this:
fun onWorldData(data:JSONObject){ // called when sv update arrives to client.
serverUpdate = data
timeSinceLastUpdate = 0f
}
fun update(){
if(serverUpdate.has("x")){ // if server update arrived
var blackPieceBodyX = Interpolation.linear.apply(blackPiece.body.position.x,serverUpdate.get("x").toString().toFloat(),timeSinceLastUpdate/updateTime)
var blackPieceBodyY = Interpolation.linear.apply(blackPiece.body.position.y,serverUpdate.get("y").toString().toFloat(),timeSinceLastUpdate/updateTime)
blackPiece.body.setTransform(Vector2(blackPieceBodyX,blackPieceBodyY),0f) //set position of body to interpolated values.
timeSinceLastUpdate += deltaTime
}
}
As you see in this example i interpolate between interpolated position of body and serverUpdate, not exactly interpolating between 2 server states inside my update method.updateTime is ping+serverUpdateRate since ping is around 70 and server sends updates every 100 ms it should be like 170(?) or this approach is completely wrong i am not sure.
Right now i am not sending timeStamp with serverUpdate from server but if it is necessary for interpolation 3rd parameter calculation i can do that.
Thanks