我在以下代码上遇到麻烦:
//Make life easier by assigning the last two relevant messages to variables
Update pos0 = m_Network->positionUpdates[m_Network->positionUpdates.size() - 1];
Update pos1 = m_Network->positionUpdates[m_Network->positionUpdates.size() - 2];
//Calculate velocities for X and Z from last two messages
velX = (pos0.posX - pos1.posX) / (pos0.timeStamp - pos1.timeStamp);
velZ = (pos0.posZ - pos1.posZ) / (pos0.timeStamp - pos1.timeStamp);
//Calculate the time for when we are trying to predict
predictionTime = totalTime - pos0.timeStamp;
//Linear prediction model to calculate where we want the car to be
D3DXVECTOR3 newPos = D3DXVECTOR3((pos0.posX + velX * predictionTime), 2.0f, (pos0.posZ + velZ * predictionTime));
//Interpolate to the new position
D3DXVec3Lerp(&position, &position, &newPos, timeSinceLastFrame);
//Set the model to where the car is
m_Model->SetPosition(position.x, position.y, position.z);
您可以看到,想法是获取从另一个客户端收到的消息,并找到对象速度以计算其位置。
我知道那部分有效,因为当我简单地将汽车的位置更新为方程式的输出时,汽车会进入原本要去的位置(以极为紧张的方式)。
当我尝试从当前位置到新位置时,汽车甚至不会出现在屏幕上。查看实际上从VE3LERP函数返回的内容,我得到的只是" -1 Indef"。
有什么想法会发生什么?
doh!我发现了。
在第二个客户的汽车开始移动之前,它处于0.0F,0.0F,0.0F。因此,前几个位置更新是这样的,导致该代码尝试除以0以获取Velx和Vely。
当然,因为我们总是只是在位置之间呆着,即使在获得适当的值以计算的值之后,这也搞砸了。
我这样做了 -
if ((isnan(newPos.x) == false) && (isnan(newPos.z) == false)) {
D3DXVec3Lerp(&position, &position, &newPos, predictionTime);
graphicsAngle = pos0.angle;
}