Sign in to follow this  
Medo Mex

Extracting LookAt Vector from View Matrix

Recommended Posts

GuyWithBeard    1892
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. Edited by GuyWithBeard

Share this post


Link to post
Share on other sites
Medo Mex    891
I tried the following code:
[CODE]
D3DXVECTOR3 getLookAt()
{
D3DXMatrixInverse(&matView, NULL, &matView);
D3DXVECTOR3 lookAtInViewSpace(0, 0, 1);
D3DXVECTOR3 lookAtInWorldSpace;
D3DXVec3TransformNormal(&lookAtInWorldSpace, &lookAtInViewSpace, &matView);
return lookAtInWorldSpace;
}
[/CODE]

It's not working.

Share this post


Link to post
Share on other sites
Mussi    4407
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 [i]m[/i] is your D3DMATRIX then your look at vector is ([i]m._13, m._23, m._33[/i]) in view space. Edited by Mussi

Share this post


Link to post
Share on other sites
Medo Mex    891
Tried the following:
[CODE]D3DXMATRIX matView = camera->viewMatrix();
D3DXVECTOR3 look(matView._13, matView._23, matView._33);[/CODE]

Not working as it should.

I even tried:[CODE]
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
[/CODE]

Share this post


Link to post
Share on other sites
Mussi    4407
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 [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb206342%28v=vs.85%29.aspx"]here[/url] and [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205342%28v=vs.85%29.aspx"]here[/url] 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? Edited by Mussi

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665
[quote name='Mussi' timestamp='1355681849' post='5011315']
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 [i]m[/i] is your D3DMATRIX then your look at vector is ([i]m._13, m._23, m._33[/i]) in view space.
[/quote]

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

Share this post


Link to post
Share on other sites
Mussi    4407
[quote name='Burnt_Fyr' timestamp='1355686896' post='5011330']

To be complete, the first column could actually be a left vector, depending on choice of coordinate systems.
[/quote]
Correct, it's actually left for DirectX(left handed) and right for OpenGL(right handed) by default.

[quote name='Medo3337' timestamp='1355688790' post='5011340']
@Mussi: I'm looking to get the vector the camera is looking at, I'm trying to extract that vector from view matrix.
[/quote]
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?

Share this post


Link to post
Share on other sites
eppo    4877
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.

Share this post


Link to post
Share on other sites
Medo Mex    891
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:
[CODE]capsuleMesh->LookAt(m_vLook.x, m_vLook.y, m_vLook.z);[/CODE]

It works.

But when I don't use D3DXMatrixLookAtLH() for matrix calculation and use the following code instead:
[CODE]
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;
[/CODE]

And then try to make the capsule look at the same direction as the camera:
[CODE]capsuleMesh->LookAt(m_vLook.x, m_vLook.y, m_vLook.z);[/CODE]
It doesn't work.

Share this post


Link to post
Share on other sites
Mussi    4407
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 [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb205342%28v=vs.85%29.aspx"]link [/url] 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. Edited by Mussi

Share this post


Link to post
Share on other sites
Medo Mex    891
I tried changing the line:
[CODE]D3DXVec3Normalize( &m_vLookAt, &m_vLookAt );[/CODE]

To:
[CODE]D3DXVec3Normalize( &m_vLookAt, &(m_vLookAt - m_vPosition) );[/CODE]

It mess up with the camera.

Share this post


Link to post
Share on other sites
Medo Mex    891
Here are the default values that I use to initialize the camera:

[CODE]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);[/CODE]

Share this post


Link to post
Share on other sites
cgdev    411
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;

Share this post


Link to post
Share on other sites
cgdev    411
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;

Share this post


Link to post
Share on other sites
Medo Mex    891
Unfortutenly, the camera is working but I couldn't get the capsule mesh to look at the same direction as the camera.

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

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. Edited by Medo3337

Share this post


Link to post
Share on other sites
Medo Mex    891
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this