• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Extracting LookAt Vector from View Matrix

27 posts in this topic

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
0

Share this post


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

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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
0

Share this post


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

Share this post


Link to post
Share on other sites
[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?
0

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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
1

Share this post


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

Share this post


Link to post
Share on other sites
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]
0

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
Don't update m_vLookAt with vLook because it is a normalized directional vector while m_vLookAt is a point vector.
0

Share this post


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

Share this post


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

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  
Followers 0