Jump to content
  • Advertisement

Archived

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

maltagate

How to let the camera look at a position

This topic is 5812 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 have programmed several camera functions. Move,Rotate,... , Now I want to write a function where I say : Camera look at x,y,z or look at object. So the idea is to give a) The camera´s position b) the Position or object´s position that has to be seen. How can I realize this? Thanks in Advance!!!! this is my Camera structur en Update Function typedef struct _S3DX8_Camera { float m_Roll; float m_Pitch; float m_Yaw; // The position D3DVECTOR m_Position; // The look at vector D3DVECTOR m_LookAt; // The up vector D3DVECTOR m_Up; // The rigth vector D3DVECTOR m_Right; // The cameras velocity D3DVECTOR m_Velocity; bool update; } S3DX8_Camera; void DX8_Camera_Update(S3DX8_Device* pDevice,S3DX8_Camera *pCamera) { D3DXMATRIX matPitch, matRoll, matYaw; D3DXMATRIX mView; pCamera->m_Position.x += pCamera->m_Velocity.x * pCamera->m_Right.x; pCamera->m_Position.y += pCamera->m_Velocity.y * pCamera->m_Right.y; pCamera->m_Position.z += pCamera->m_Velocity.z * pCamera->m_Right.z; pCamera->m_Position.x += pCamera->m_Velocity.x * pCamera->m_Up.x; pCamera->m_Position.y += pCamera->m_Velocity.y * pCamera->m_Up.y; pCamera->m_Position.z += pCamera->m_Velocity.z * pCamera->m_Up.z; pCamera->m_Position.x += pCamera->m_Velocity.x * pCamera->m_LookAt.x; pCamera->m_Position.y += pCamera->m_Velocity.y * pCamera->m_LookAt.y; pCamera->m_Position.z += pCamera->m_Velocity.z * pCamera->m_LookAt.z; D3DXVec3Normalize( &pCamera->m_LookAt, &pCamera->m_LookAt ); D3DXVec3Cross(&pCamera->m_Right,&pCamera->m_Up,&pCamera->m_LookAt); D3DXVec3Normalize( &pCamera->m_Right, &pCamera->m_Right ); D3DXVec3Cross(&pCamera->m_Up,&pCamera->m_LookAt,&pCamera->m_Right); D3DXVec3Normalize( &pCamera->m_Up, &pCamera->m_Up ); D3DXMatrixRotationAxis( &matYaw, &pCamera->m_Up, pCamera->m_Yaw ); D3DXMatrixRotationAxis( &matPitch, &pCamera->m_Right, pCamera->m_Pitch ); D3DXMatrixRotationAxis( &matRoll, &pCamera->m_LookAt, pCamera->m_Roll ); D3DXVec3TransformCoord( &pCamera->m_LookAt, &pCamera->m_LookAt, &matYaw ); D3DXVec3TransformCoord( &pCamera->m_Right, &pCamera->m_Right, &matYaw ); D3DXVec3TransformCoord( &pCamera->m_LookAt, &pCamera->m_LookAt, &matPitch ); D3DXVec3TransformCoord( &pCamera->m_Up, &pCamera->m_Up, &matPitch ); D3DXVec3TransformCoord( &pCamera->m_Right, &pCamera->m_Right, &matRoll ); D3DXVec3TransformCoord( &pCamera->m_Up, &pCamera->m_Up, &matRoll ); D3DXMatrixIdentity( &mView ); mView._11=pCamera->m_Right.x; mView._12=pCamera->m_Up.x; mView._13=pCamera->m_LookAt.x; mView._14=0; mView._21=pCamera->m_Right.y; mView._22=pCamera->m_Up.y; mView._23=pCamera->m_LookAt.y; mView._24=0; mView._31=pCamera->m_Right.z; mView._32=pCamera->m_Up.z; mView._33=pCamera->m_LookAt.z; mView._34=0; mView._41=- D3DXVec3Dot( &pCamera->m_Position, &pCamera->m_Right ); mView._42=- D3DXVec3Dot( &pCamera->m_Position, &pCamera->m_Up ); mView._43=- D3DXVec3Dot( &pCamera->m_Position, &pCamera->m_LookAt ); mView._44=1; pDevice->pd3dDevice->lpVtbl->SetTransform(pDevice->pd3dDevice,D3DTS_VIEW,&mView); }

Share this post


Link to post
Share on other sites
Advertisement
Yep. Use the d3dx method. Or you could do it the full mathematical way (actually what D3DXMatrixLookAtLH does anyway)which I will explain if you want. I prefer to do it the maths wat because then at least I understand what is going on. Plus that means that if you want to port your game at a later point to another library you find yourself with platform-independant camera code already.

Rory.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!