c++directx-10lerp

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


I'm having trouble with the following code:

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

As you can see, the idea is to take messages that have been received from another client, and find the objects velocity in order to calculate its position.

I know that that part works, because when I simply update the position of the car to the output from the equation, the car goes where it's meant to go (in an extremely jittery fashion).

When I attempt to lerp from the current position to the new position however, the car doesnt even appear on screen. Looking at whats actually being returned from the Ve3Lerp function, all I'm getting is "-1 INDEF".

Any ideas what could be up?


Solution

  • Doh! I figured it out.

    Before the car on the second client started moving, it was sitting at 0.0f, 0.0f, 0.0f. So the first few position updates were coming through as that, leading this code to try and divide by 0 to get velX and velY.

    Of course, as we were always just lerping between positions, this screwed up every future position, even after getting proper values to calculate with.

    I got around it by doing this -

    if ((isnan(newPos.x) == false) && (isnan(newPos.z) == false)) {
        D3DXVec3Lerp(&position, &position, &newPos, predictionTime);
        graphicsAngle = pos0.angle;
    }