Extracting LookAt Vector from View Matrix

Started by
26 comments, last by JohnnyCode 11 years, 4 months ago
Hi guys,

How do I extract LookAt vector from camera view matrix (D3DXMATRIX)?
Advertisement
Do you want the look at vector in world space? If so, assuming you have a left handed coordinate system you can do the following:

In pseudo-code:

Matrix viewMatrix = camera->getViewMatrix();
viewMatrix.invert();
Vector3 lookAtInViewSpace(0, 0, 1); // Forward vector (use 0, 0, -1 if you have a right-handed coordinate system)
Vector3 lookAtInWorldSpace = lookAtInViewSpace * viewMatrix; // Transform into world space.

I am not that familiar with D3DX (which you seem to be using) but I think the transformation function you want is D3DXVec3TransformCoord.

UPDATE: No sorry, I was wrong. It is D3DXVec3TransformNormal, since you are transforming a direction, not a point.
I tried the following code:

D3DXVECTOR3 getLookAt()
{
D3DXMatrixInverse(&matView, NULL, &matView);
D3DXVECTOR3 lookAtInViewSpace(0, 0, 1);
D3DXVECTOR3 lookAtInWorldSpace;
D3DXVec3TransformNormal(&lookAtInWorldSpace, &lookAtInViewSpace, &matView);
return lookAtInWorldSpace;
}


It's not working.
Just extract the vector from column 3 of the view matrix
Like Asesh said, the third column of the matrix represents the look at vector, the first and second being the right and up vectors. If m is your D3DMATRIX then your look at vector is (m._13, m._23, m._33) in view space.
Tried the following:
D3DXMATRIX matView = camera->viewMatrix();
D3DXVECTOR3 look(matView._13, matView._23, matView._33);


Not working as it should.

I even tried:
D3DXMatrixLookAtLH(&matView,
&m_vPosition,
&m_vLook,
&m_vUp);

D3DXVECTOR3 lookResult(matView._13, matView._23, matView._33); // Extract m_vLook from the above view matrix
// Invalid result, lookResult is not equal to m_vLook
How is it not working and how do think it should be working?

D3DMatrixLookAtLH is not doing what you think it does. It computes a view matrix based on an eye position, a point to look at and an up direction. The direction you look in is based on the eye position and look at position, so your m_vLook will generally not be the same as the direction you're looking in. m_vLook is a position, not a direction.

Have a look here and here for some more information.

Edit: maybe there's some confusion about what you're trying to accomplish, are you looking for the direction you're looking in? What are you trying to accomplish?

Like Asesh said, the third column of the matrix represents the look at vector, the first and second being the right and up vectors. If m is your D3DMATRIX then your look at vector is (m._13, m._23, m._33) in view space.


To be complete, the first column could actually be a left vector, depending on choice of coordinate systems.
@Mussi: I'm looking to get the vector the camera is looking at, I'm trying to extract that vector from view matrix.


To be complete, the first column could actually be a left vector, depending on choice of coordinate systems.

Correct, it's actually left for DirectX(left handed) and right for OpenGL(right handed) by default.


@Mussi: I'm looking to get the vector the camera is looking at, I'm trying to extract that vector from view matrix.

By that you mean the direction? If so, the solution I posted in my first post is what you're looking for. What are you trying to do and what results are you getting?

This topic is closed to new replies.

Advertisement