Jump to content
  • Advertisement
Sign in to follow this  

Moving camera along tangent

This topic is 948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm playing around with height maps. I'm trying to move the camera along the tangent as a walk on hilly terrain. So far my code is:

XMFLOAT3 camPos = mCam.GetPosition(); //current position of camera on flat grid
 float y = mTerrain.GetHeight(camPos.x, camPos.z); //height
 XMVECTORF32 oldpos = {camPos.x,camPos.y,camPos.z}; 
XMVECTORF32 newpos = {camPos.x,y,camPos.z};
 XMVECTOR ntangent = XMVector3Normalize(newpos - oldpos);
 XMFLOAT3 tangent; XMStoreFloat3(&tangent,ntangent); 
mCam.SetPosition(camPos.x, tangent.y + 2.0f, camPos. z);

But my results do not go along the terrain. they go under the terrain. I cant find the problem and need help. Does anyone know why my code does not work?


Share this post

Link to post
Share on other sites


if( mWalkCamMode )
  XMFLOAT3 camPos = mCam.GetPosition();
  float y = mTerrain.GetHeight(camPos.x, camPos.z);
  mCam.SetPosition(camPos.x, y, camPos.z);
  XMFLOAT3 tupac = mCam.GetPosition();
  float one = tupac.x - camPos.x;
  float two = tupac.y - camPos.y;
  float three = tupac.z - camPos.z;
  float test = sqrt((one*one)+(two*two)+(three*three));
  float one2 = one/test;
  float two2 = two/test;
  float three2 = three/test;
  mCam.SetPosition(one2, two2, three2);


still goes under terrain

Share this post

Link to post
Share on other sites

I found this code right here that ill dissect and implement in my code.


void Camera::update(float dt, Terrain* terrain, float offsetHeight)


// Find the net direction the camera is traveling in (since the

// camera could be running and strafing).

D3DXVECTOR3 dir(0.0f, 0.0f, 0.0f);

if( gDInput->keyDown(DIK_W) )

  dir += mLookW;

if( gDInput->keyDown(DIK_S) )

  dir -= mLookW;

if( gDInput->keyDown(DIK_D) )

  dir += mRightW;

if( gDInput->keyDown(DIK_A) )

  dir -= mRightW;


// Move at mSpeed along net direction.

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;




  mPosW = newPos;



// We rotate at a fixed speed.

float pitch  = gDInput->mouseDY() / 150.0f;

float yAngle = gDInput->mouseDX() / 150.0f;


// Rotate camera's look and up vectors around the camera's right vector.


D3DXMatrixRotationAxis(&R, &mRightW, pitch);

D3DXVec3TransformCoord(&mLookW, &mLookW, &R);

D3DXVec3TransformCoord(&mUpW, &mUpW, &R);


// Rotate camera axes about the world's y-axis.

D3DXMatrixRotationY(&R, yAngle);

D3DXVec3TransformCoord(&mRightW, &mRightW, &R);

D3DXVec3TransformCoord(&mUpW, &mUpW, &R);

D3DXVec3TransformCoord(&mLookW, &mLookW, &R);


// Rebuild the view matrix to reflect changes.



mViewProj = mView * mProj;


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!