🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Interpolation on Client Between Server States

Started by
12 comments, last by lifesuxtr 4 years, 7 months ago

My bad i forgot to add += it should be

timeSinceLastUpdate += dt*1000

So i should add serverUpdates to an ArrayList, and wait until it has lets say 6 updates and then start interpolating inside my update() from 0 to 1, then 1 to 2, 3 to 4 4 to 5 so on right ?


Advertisement

Assuming you're asking about the full timestamped route - you don't need to wait for 6 updates. If the updates have timestamps then you just need to ensure that you always have at least 1 update timestamped before your local simulation time and one timestamped after your local simulation time, so that you can find interpolated values for the current time. Ideally you wouldn't really have more than 3 in the buffer at most (2 you're interpolating between, and 1 that you're about to start using once your local simulation time passes that of the 2nd update of the 3).

If you're not ready for that complexity and don't need a perfectly smooth response, you can just treat updates as a moving target - when a new update comes in, mark it as "where I need this object to be in 150ms time" or some other value, then you can interpolate from the current position to that future position over the next 150ms. And when another update comes in, you shift to interpolating towards that one, and so on.

I did this looks like it is performing ok but not perfect. Because i couldnt understand implementing it with deltaTime i created seperate thread for sleep 1 milisecond inside while

fun onServerUpdate(data){
serverUpdates.add(data)
}
  Thread(Runnable {
      var index = 0
      while(true){

        if(serverPositions.size >=6){
          var elapsedTime = 0f
          var update1 = serverPositions.get(index)
          var update2 = serverPositions.get(index+1)

          var update1X = update1.get("x").toString().toFloat()
          var update1Y = update1.get("y").toString().toFloat()


          var update2X = update2.get("x").toString().toFloat()
          var update2Y = update2.get("y").toString().toFloat()

          var timeDifference = update2.get("time").toString().toLong() - update1.get("time").toString().toLong() // time is timestamp milisecond

          while(elapsedTime < timeDifference){
            var shooterX = Interpolation.linear.apply(update1X,update2X,elapsedTime/timeDifference)
            var shooterY = Interpolation.linear.apply(update1Y,update2Y,elapsedTime/timeDifference)


            shooter.body.setTransform(Vector2(shooterX,shooterY ),0f)

            Thread.sleep(1)
            elapsedTime +=1


          }

          index++
        }

      }

    }).start()


This topic is closed to new replies.

Advertisement