Archived

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.

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 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.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633702
• Total Posts
3013448
×