Advertisement Jump to content


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


Getting Camera angles?

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

Gidday, Hope you are all well. I have been playing with a Camera class that I found on the net some time ago, it works fine. I now need to extract the camera rotation angle, I know how to convert from radians to degrees already, but the problem I have with the attached code is, so far I am using m_vFacing.z. Now this gives me 1.0 facing forward to +z, if I turn 45 deg it is now 0.5, 90deg = 0.0 135 deg -0.5 180 deg -1.0 225 deg -0.5 and so on. How could I convert this to 0 - 360 degrees, or just plain radians? HRESULT hr; D3DXMATRIX matTotal; D3DXMATRIX matRotAboutUp, matRotAboutRight, matRotAboutFacing; // bail if we haven''t moved since the last update if (!m_bMovedSinceLastUpdate){ return S_OK; } // get the rotation matrices for each rotation D3DXMatrixRotationAxis(&matRotAboutRight,&m_vRight,m_fRotAboutRight); D3DXMatrixRotationAxis(&matRotAboutUp,&m_vUp,m_fRotAboutUp); D3DXMatrixRotationAxis(&matRotAboutFacing,&m_vFacing,m_fRotAboutFacing); // concatenate them to form a total rotation matrix D3DXMatrixMultiply(&matTotal,&matRotAboutUp,&matRotAboutRight); D3DXMatrixMultiply(&matTotal,&matRotAboutFacing,&matTotal); // transform 2 of the vectors by this matrix and get the third using the cross product D3DXVec3TransformCoord(&m_vRight,&m_vRight,&matTotal); D3DXVec3TransformCoord(&m_vUp,&m_vUp,&matTotal); D3DXVec3Cross(&m_vFacing,&m_vRight,&m_vUp); // are these two still perpandicular? if (fabs(D3DXVec3Dot(&m_vUp,&m_vRight)) > 0.01){ // these suckers aren''t perpandicular anymore - get another using the cross product D3DXVec3Cross(&m_vUp,&m_vFacing,&m_vRight); } // remember to normalise vectors before putting them into the view matrix D3DXVec3Normalize(&m_vRight,&m_vRight); D3DXVec3Normalize(&m_vUp,&m_vUp); D3DXVec3Normalize(&m_vFacing,&m_vFacing); // used to compute the bottom row of the view matrix float fView41,fView42,fView43; fView41 = -D3DXVec3Dot(&m_vRight,&m_vPosition); fView42 = -D3DXVec3Dot(&m_vUp,&m_vPosition); fView43 = -D3DXVec3Dot(&m_vFacing,&m_vPosition); m_matView = D3DXMATRIX( m_vRight.x, m_vUp.x, m_vFacing.x, 0.0f, m_vRight.y, m_vUp.y, m_vFacing.y, 0.0f, m_vRight.z, m_vUp.z, m_vFacing.z, 0.0f, fView41, fView42, fView43, 1.0f ); // update the device - if we fail then return the error code hr = MyMilk3.m_pd3dDevice->SetTransform(D3DTS_VIEW,&m_matView); if (FAILED(hr)){ return hr; } // reset the various variables to reflect the new update m_fRotAboutUp = m_fRotAboutRight = m_fRotAboutFacing = 0.0f; m_bMovedSinceLastUpdate = false; return S_OK; Thanks in advance for any help TakeCare

Share this post

Link to post
Share on other sites
that''s the normalize at vector, you need an axis of reference and an angle of reference then just use 2 of the 3 components of the at vector (x,y,z) that you mention and a litle bit of geo/trig and you got your angle.

Share this post

Link to post
Share on other sites
Thanks for your response nhatkthanh,

what do yo mean by axis of reference?
Is that my Up y reference ?

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!