我正在尝试制作客户端-服务器架构。我被困在插值部分。现在,我有一个非常幼稚的插值算法实现。我让每个玩家都有一个位置历史记录,每当我从服务器收到其他玩家的位置数据时,我都会将位置推送到该数组中。我使用最旧的位置历史记录以恒定速度插入到新位置的每个客户端框架。
// when new position for other player recieved
p.stateHistory.push(data)
// Every client frame
if(p.stateHistory.length < 1)
return false
let deltaPosition = p.stateHistory[0].position.clone().sub(p.clientPosition)
let direction = Math.atan2(deltaPosition.y, deltaPosition.x)
let velocity = new Vector2(Math.cos(direction), Math.sin(direction)).scale(30/100)
let threshold = 10
if(deltaPosition.magnitude() < threshold) {
p.clientPosition.x = p.stateHistory[0].position.x
p.clientPosition.y = p.stateHistory[0].position.y
p.stateHistory.shift()
} else {
p.clientPosition.add(velocity.clone().scale(deltaTime))
}
我找不到其他方法以恒定速度进行插值。我从 gafferongames 开始了解 Hermite 插值。但很遗憾,这篇文章没有任何关于它的数学和实现的信息。我试图通过关于 Hermite 插值的维基百科文章,但它没有帮助。我对它背后的数学一无所知。一个伪代码将不胜感激。
到目前为止我能做的:http: //client-side-prediction-attempt.herokuapp.com/