Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Camera to face a target vector

This topic is 5823 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

Hi all, I''ve built a 3rd person camera that rotates yaw and pitch around a vector position. I am however having trouble getting the camera to constantly face the target vector while i rotate i have some of my sample code. I left the rotation of the final matrix as the identity and only set the position of the camera matrix. any help would be greatly appreciated. thanks void CCamera::Render(CRenderer* pRenderer, D3DXVECTOR3 &vMeshPos, float fRadius) { CMat4 mYaw,mPitch,mView,mRes; CVector3 vPos = CVector3(0,0,-fRadius); // concatenate the rotaion matrices to get the // rotation around the object mYaw.RotateY(m_fYawRotation); mPitch.RotateX(m_fPitchRotation); mRes = mPitch.MulMat4(mYaw); // get the position for the camera vPos = mRes.MulVec3(vPos); // Set the position of the camera mView._41 = -vPos.x; mView._42 = -vPos.y; mView._43 = -vPos.z; pRenderer->GetDevice()->SetTransform(D3DTS_VIEW,(D3DXMATRIX*)&mView); }

Share this post

Link to post
Share on other sites
The way I would do this is as follows:

1) set the position of the camera matrix directly
2) calculate a vector from the camera position to the target, and make it a unit vector
3) assign the Z axis of the camera matrix (3rd column, mView._31, mView._32, mView._33) to be equal to the just calculated vector
4) assume an up direction, say (0,1,0), and assign that to the Y axis of the camera matrix (2nd column, mView._21, mView._22, mView._23).
5) calculate an X axis as Y.Cross(Z).
6) assign the calculated X axis so the X axis of the camera matrix (1st column, mView._11, mView._12, mView._13).

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

Share this post

Link to post
Share on other sites

  • 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!