Direct3D - How to calculate Roll from View Matrix?

Hey guys, this one has been eating all night, and I finally raise my hands for help. Basically, itโ€™s pretty simple to calculate Pitch and Yaw from the viewing matrix immediately after updating the camera:

D3DXMatrixLookAtLH(&m_View, &sCam.pos, &vLookAt, &sCam.up); pDev->SetTransform(D3DTS_VIEW, &m_View); // Set the camera axes from the view matrix sCam.right.x = m_View._11; sCam.right.y = m_View._21; sCam.right.z = m_View._31; sCam.up.x = m_View._12; sCam.up.y = m_View._22; sCam.up.z = m_View._32; sCam.look.x = m_View._13; sCam.look.y = m_View._23; sCam.look.z = m_View._33; // Calculate yaw and pitch and roll float lookLengthOnXZ = sqrtf( sCam.look.z^2 + sCam.look.x^2 ); fPitch = atan2f( sCam.look.y, lookLengthOnXZ ); fYaw = atan2f( sCam.look.x, sCam.look.z ); 

So my problem is: there must be some way to get Roll in radians, just like Pitch and Yaw get at the end of the code there. I tried several dozen algorithms that seemed to make sense to me, but none of them gave the desired result. I understand that many developers do not track these values, however, I am writing some procedures to reorient and adjust the crop based on the values, and manual tracking breaks when you apply mixed rotations to the View Matrix. So, does anyone have a formula for getting Roll in radians (or degrees, I don't care) from View Matrix? Thanks!

+4
source share
2 answers

It seems the Wikipedia article about the Euler angles contains the formula, you are looking at the end.

0
source

Woooh, got it! Thanks, stacker for linking to Euler Angles Wikipedia entry, the gamma formula was really the right solution in the end! So, to calculate Roll in radians from the โ€œvertical planeโ€, I use the following line of C ++ code:

 fRoll = atan2f( sCam.up.y, sCam.right.y ) - D3DX_PI/2; 

As Euler pointed out, you are looking for the arc tangent of the matrix Y (up) of the matrix Z with respect to the value X (on the right) of the matrix Z, although in this case I tried the values โ€‹โ€‹of Z and they did not give the desired result, so I tried the values โ€‹โ€‹of Y on a whim, and got a value that was turned off by + PI / 2 radians (right corner to the desired value), which is easily compensated. Finally, I have a gryscopically accurate and stable 3D camera platform with a self-adjusting balance. =)

+3
source

Source: https://habr.com/ru/post/1303388/


All Articles