• Create Account

## Useful little code: Vector orientation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

No replies to this topic

### #1pachesantiago  Members

188
Like
0Likes
Like

Posted 15 October 2012 - 11:31 AM

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, 15 October 2012 - 11:39 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.