• Advertisement
Sign in to follow this  

Useful little code: Vector orientation

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

Hy everyone. I'm posting this little code i put toghether today ( taken from DXUT and modifed acctually), wich i had been serching for a long time.
It allows to orientate an object using a target vector and a postion vector.
Here it is :


D3DXVECTOR2 GetYawPitchFromVectors(D3DXVECTOR3* pvEyePt, D3DXVECTOR3* pvLookatPt)
{
D3DXMATRIX m_mView;
// Calc the view matrix
D3DXVECTOR3 vUp( 0,1,0 );
D3DXMatrixLookAtLH( &m_mView, pvEyePt, pvLookatPt, &vUp );
D3DXMATRIX mInvView;
D3DXMatrixInverse( &mInvView, NULL, &m_mView );
// The axis basis vectors and camera position are stored inside the
// position matrix in the 4 rows of the camera's world matrix.
// To figure out the yaw/pitch of the camera, we just need the Z basis vector
D3DXVECTOR3* pZBasis = ( D3DXVECTOR3* )&mInvView._31;
D3DXVECTOR2 data;
data.x = atan2f( pZBasis->x, pZBasis->z );
float fLen = sqrtf( pZBasis->z * pZBasis->z + pZBasis->x * pZBasis->x );
data.y = -atan2f( pZBasis->y, fLen );
return data;
}
Edited by pachesantiago

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement