DirectX Camera rotation

Started by
4 comments, last by Evil Steve 17 years, 1 month ago
Can anyone please describe how to rotate the camera around the volume using DirectX.I am setting the view matrix to set the position of camera.But no idea about how to rotate the camera around the object.
Advertisement
Are you using Native or Managed DirectX?
Native.Thanks in advance.
the way i do it (prob not the best):

cameraPos = targetPos + Vector3.TransformCoordinate( forwardDir, Matrix.RotationYawPitchRoll( yaw, pitch, 0.0f ) ) * radius;
I tried to change the camera position as you suggested then my volume is going out of screen.

here is my code to set view matrix and projection matrix.

//Set the View Matrix
D3DXMATRIXA16 matView;
D3DXVECTOR3 vFromPt = D3DXVECTOR3( 0.0f, 0.0f, -3.0f );
D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMATRIX yawpitchMatrix;
D3DXMatrixRotationYawPitchRoll &yawpitchMatrix,m_stRenderingParams.m_fOrbitY,m_stRenderingParams.m_fOrbitX,0.0);
D3DXVec3TransformCoord(&vUpVec ,&vUpVec , &yawpitchMatrix);

vFromPt = vLookatPt + vUpVec * 1; D3DXMatrixLookAtLH( &matView, &vFromPt, &vLookatPt, &vUpVec );
m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
//Orthogonal
// Set the projection matrix
D3DXMATRIX matProj;
FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
D3DXMatrixOrthoLH( &matProj,fAspect,1.0f,-5.0f, 5.0f );
m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
You don't usually want to rotate the camera around the object, you want to rotate the object around the camera.

Set your view matrix up as normal, at say (0, 0, -10) looking at (0, 0, 0), and then each frame you do the following (Assuming your object is centred at (0, 0, 0):
D3DXMATRIX matRot, matTrans, matFinal;D3DXMatrixRotationY(&matRot, fAngle);D3DXMatrixTranslation(&matTrans, 0.0f, 0.0f, 25.0f);D3DXMatrixMultiply(&matFinal, &matRot, &matTrans);pDevice->SetTransform(D3DTS_WORLD, &matFinal);

Where fAngle is the angle to rotate the object by, in radians (You'll want to increase it each frame).


I'd recommend you read up on matrix maths a bit before going much further, or you'll just get more and more confused.

This topic is closed to new replies.

Advertisement