Extracting LookAt Vector from View Matrix

Started by
26 comments, last by JohnnyCode 11 years, 3 months ago
Try again, I believe it will work:

D3DXMATRIX m_mView;
D3DXMatrixIdentity( &m_mView );
D3DXVECTOR3 vLook = m_vLookAt;
D3DXVec3Normalize( &vLook, &(vLook - m_vPosition) );
D3DXVECTOR3 vRight = m_vRight;
D3DXVec3Cross( &vRight, &m_vUp, &vLook );
D3DXVec3Normalize( &vRight, &vRight );
D3DXVECTOR3 vUp = m_vUp;
D3DXVec3Cross( &vUp, &vLook, &vRight );
D3DXVec3Normalize( &vUp, &vUp );
m_mView._11 = vRight.x;
m_mView._12 = vUp.x;
m_mView._13 = vLook.x;
m_mView._14 = 0.0f;
m_mView._21 = vRight.y;
m_mView._22 = vUp.y;
m_mView._23 = vLook.y;
m_mView._24 = 0.0f;
m_mView._31 = vRight.z;
m_mView._32 = vUp.z;
m_mView._33 = vLook.z;
m_mView._34 = 0.0f;
m_mView._41 = -D3DXVec3Dot( &m_vPosition, &vRight );
m_mView._42 = -D3DXVec3Dot( &m_vPosition, &vUp );
m_mView._43 = -D3DXVec3Dot( &m_vPosition, &vLook );
m_mView._44 = 1.0f;
Advertisement
Unfortutenly, the camera is working but I couldn't get the capsule mesh to look at the same direction as the camera.

I tried:
capsuleMesh->LookAt(camera->m_vLook.x, camera->m_vLook.y, camera->m_vLook.z);

The above code works perfectly only when I calculate the view matrix using D3DXMatrixLookAtLH() instead of the code you posted.

I'm trying to use m_vRight so I can't just use D3DXMatrixLookAtLH() to calculate the view matrix.
Don't update m_vLookAt with vLook because it is a normalized directional vector while m_vLookAt is a point vector.
@Alin: I used the exact code you posted to get the view matrix, the last code you posted doesn't change m_vLookAt, it take instance of it, I never add m_vLookAt = Look.
To be clear, the only problem I am facing now is making the capsule mesh look at the same direction as the camera, If someone can show how to make the capsule mesh look at the same direction as the camera, the question will be resolved.
Create the following matrix:
[ cam._11 cam._12 cam._13 0.0]
[ cam._21 cam._22 cam._23 0.0]
[ cam._31 cam._32 cam._33 0.0]
[ 0.0 0.0 0.0 1.0]


Multiply the object’s world matrix by this matrix to get the final world matrix.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Resolved!

I had to set the capsule LookAt to (m_vPosition + m_vLook).

Thanks everyone who participated in the thread!

maybe you could transform 0,0,1 vector (or any) by the view matrix. Resulting vector should be pointing in the direction of view, considered it to be a world space vector. It might be performance wiser operation then decomposing view matrix.

This topic is closed to new replies.

Advertisement