I cant seem to get this code working,its suppose to let a first person style camera jump up and back down,and land back on the terrains y heightmap,thanks if you can help
//in camera.cpp
D3DXVec3Normalize(&dir, &dir);
D3DXVECTOR3 newPos = mPosW + dir*mSpeed*dt;
if( terrain != 0)
{
// New position might not be on terrain, so project the
// point onto the terrain.
//newPos.y = terrain->getHeight(newPos.x, newPos.z) + offsetHeight;
// Now the difference of the new position and old (current)
// position approximates a tangent vector on the terrain.
D3DXVECTOR3 tangent = newPos - mPosW;
D3DXVec3Normalize(&tangent, &tangent);
// Now move camera along tangent vector.
mPosW += tangent*mSpeed*dt;
// After update, there may be errors in the camera height since our
// tangent is only an approximation. So force camera to correct height,
// and offset by the specified amount so that camera does not sit
// exactly on terrain, but instead, slightly above it.
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
if(canJump)
{
static float timeadded;
timeadded += dt;
float CurJump=JUMPAMP * sin(timeadded * D3DX_PI / JUMPTIME);//Store the height of the jump in progress to avoid 2 calls to sin()
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight + CurJump;
if (CurJump<0) //If we have reached the ground lock the height back to the terrain and end the jump with jump=0
{
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
canJump = false;
timeadded = 0;
}
else
{
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
}
}
else
{
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
}
}
else
{
mPosW = newPos;
}