为什么这个 DirectX 10 "D3DXVec3Lerp" 函数 return 无效数字?

Why does this DirectX 10 "D3DXVec3Lerp" function return invalid numbers?

我在使用以下代码时遇到问题:

//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);

如您所见,我们的想法是获取从另一个客户端接收到的消息,并找到对象速度以计算其位置。

我知道那部分是有效的,因为当我简单地将汽车的位置更新为方程式的输出时,汽车就会到达它应该去的地方(以一种非常紧张的方式)。

然而,当我尝试从当前位置 lerp 到新位置时,汽车甚至没有出现在屏幕上。查看 Ve3Lerp 函数实际返回的内容,我得到的只是“-1 INDEF”。

有什么想法吗?

呸!我想通了。

在第二个客户端上的汽车开始移动之前,它位于 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;
}