Extracting LookAt Vector from View Matrix

Started by
26 comments, last by JohnnyCode 11 years, 3 months ago
I believe he is looking for the original m_vLook argument you pass as the third argument to D3DXMatrixLookAtLH().

Unfortunately, that information is lost since the third column in the view matrix is vLook - vPosition normalized. You only know the line along which the original LookAt point lies.
Advertisement
Let me make things more clear, I'm trying to rotate a capsule mesh to make it look at the same direction as the camera, the capsule mesh is the player.

So, when I use D3DXMatrixLookAtLH() for view matrix calculation, I can do something like the following to make the capsule look at the same direction the camera is looking at:
capsuleMesh->LookAt(m_vLook.x, m_vLook.y, m_vLook.z);

It works.

But when I don't use D3DXMatrixLookAtLH() for matrix calculation and use the following code instead:

D3DXMATRIX m_mView;
D3DXMatrixIdentity( &m_mView );
D3DXVec3Normalize( &m_vLookAt, &m_vLookAt );
D3DXVec3Cross( &m_vRight, &m_vUp, &m_vLookAt );
D3DXVec3Normalize( &m_vRight, &m_vRight );
D3DXVec3Cross( &m_vUp, &m_vLookAt, &m_vRight );
D3DXVec3Normalize( &m_vUp, &m_vUp );
m_mView._11 = m_vRight.x;
m_mView._12 = m_vUp.x;
m_mView._13 = m_vLookAt.x;
m_mView._14 = 0.0f;
m_mView._21 = m_vRight.y;
m_mView._22 = m_vUp.y;
m_mView._23 = m_vLookAt.y;
m_mView._24 = 0.0f;
m_mView._31 = m_vRight.z;
m_mView._32 = m_vUp.z;
m_mView._33 = m_vLookAt.z;
m_mView._34 = 0.0f;
m_mView._41 = -D3DXVec3Dot( &position, &m_vRight );
m_mView._42 = -D3DXVec3Dot( &position, &m_vUp );
m_mView._43 = -D3DXVec3Dot( &position, &m_vLookAt );
m_mView._44 = 1.0f;


And then try to make the capsule look at the same direction as the camera:
capsuleMesh->LookAt(m_vLook.x, m_vLook.y, m_vLook.z);
It doesn't work.
replace the third line with: D3DXVec3Normalize( &m_vLookAt, &(m_vLookAt - m_vPosition) );
Okay, let me start by saying that your mixed use of 'look at' and 'look' is confusing. Use look for directions and look at for positions or distinguish them in some other way.

This link I posted before, shows how the view matrix is built. You want to build the view matrix with the look direction, you get this by subtracting the eye position from what point you're looking at and then normalizing that. Basically, what eppo said.
I tried changing the line:
D3DXVec3Normalize( &m_vLookAt, &m_vLookAt );

To:
D3DXVec3Normalize( &m_vLookAt, &(m_vLookAt - m_vPosition) );

It mess up with the camera.
Okay, now what? You're going to have to provide a bit more information than it messes up the camera.
It's making the camera in a position like the player is dead.
Here are the default values that I use to initialize the camera:

D3DXVECTOR3 m_vPosition(0.0f, 40.0f, 10.0f)
D3DXVECTOR3 m_vLook(0.0f, 0.0f, 1.0f)
D3DXVECTOR3 m_vUp(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 m_vRight(-1.0f, 0.0f, 0.0f);
Use the following code :

D3DXMATRIX m_mView;
D3DXMatrixIdentity( &m_mView );
D3DXVECTOR3 vLook = m_vLookAt;
D3DXVec3Normalize( &vLook, &(vLook - m_vPosition) );
D3DXVec3Cross( &m_vRight, &m_vUp, &vLook );
D3DXVec3Normalize( &m_vRight, &m_vRight );
D3DXVec3Cross( &m_vUp, &vLook, &m_vRight );
D3DXVec3Normalize( &m_vUp, &m_vUp );
m_mView._11 = m_vRight.x;
m_mView._12 = m_vUp.x;
m_mView._13 = vLook.x;
m_mView._14 = 0.0f;
m_mView._21 = m_vRight.y;
m_mView._22 = m_vUp.y;
m_mView._23 = vLook.y;
m_mView._24 = 0.0f;
m_mView._31 = m_vRight.z;
m_mView._32 = m_vUp.z;
m_mView._33 = vLook.z;
m_mView._34 = 0.0f;
m_mView._41 = -D3DXVec3Dot( &position, &m_vRight );
m_mView._42 = -D3DXVec3Dot( &position, &m_vUp );
m_mView._43 = -D3DXVec3Dot( &position, &vLook );
m_mView._44 = 1.0f;
@Alin: Tried that, the camera is up side down.

This topic is closed to new replies.

Advertisement