Sign in to follow this  
Ripiz

C++, DirectX9, mouse position to ray?

Recommended Posts

if(MouseState.rgbButtons[DIMOUSE_LEFTBUTTON] & 0x80){
POINT ptCursor;
GetCursorPos( &ptCursor );
ScreenToClient( han_Window, &ptCursor );
Matrix projMat;
p_Device->GetTransform(D3DTS_PROJECTION,&projMat);
D3DVIEWPORT9 view;
p_Device->GetViewport( &view );
Vector v;
v.x =  ( ( ( 2.0f * ptCursor.x ) / view.Width  ) - 1 ) / projMat._11;
v.y = -( ( ( 2.0f * ptCursor.y ) / view.Height ) - 1 ) / projMat._22;
v.z =  1.0f;
Matrix viewMat;
D3DXMatrixInverse( &viewMat, NULL, &projMat);
Vector rayOrigin,rayDir;
rayDir.x  = v.x*viewMat._11 + v.y*viewMat._21 + v.z*viewMat._31;
rayDir.y  = v.x*viewMat._12 + v.y*viewMat._22 + v.z*viewMat._32;
rayDir.z  = v.x*viewMat._13 + v.y*viewMat._23 + v.z*viewMat._33;
rayOrigin.x = viewMat._41;
rayOrigin.y = viewMat._42;
rayOrigin.z = viewMat._43;
LPDIRECT3DVERTEXBUFFER9 pVB;
LPDIRECT3DINDEXBUFFER9 pIB;
terrain->Mesh->GetVertexBuffer( &pVB );
terrain->Mesh->GetIndexBuffer( &pIB );
WORD* pIndices;
D3DVERTEX* pVertices;
pIB->Lock( 0, 0, ( void** )&pIndices, 0 );
pVB->Lock( 0, 0, ( void** )&pVertices, 0 );
DWORD dwFace;
FLOAT fBary1, fBary2, fDist;
D3DXIntersect( terrain->Mesh, &rayOrigin, &rayDir, &picked, &dwFace, &fBary1, &fBary2, &fDist, NULL, NULL );
}
I'm trying to make Ray from my camera, towards mouse where I clicked (or about that). Basically I need to make my terrain clickable, so I can get coordinates on terrain where I clicked, and make my character move there. However it didn't work, I made it to draw line, to see where my Ray goes - it goes from around middle of my terrain (either world coordinates, terrain is at 0), and goes to the side, parallel to terrain (or very low angle). Maybe anyone could help please? Thank you P.S. If there is some information missing, just ask [Edited by - Ripiz on September 14, 2009 11:25:40 AM]

Share this post


Link to post
Share on other sites
I think your viewMat should be the inverse of the view matrix, not the projection matrix.

As an aside, you lock the vertex and index buffers before you call D3DXIntersect. Do you unlock them at some point? That probably doesn't affect the pick but may not be good practice.

Share this post


Link to post
Share on other sites
Thanks for reply. I'm not sure what Locking does, it was pretty much copy/paste from tutorial. Other person gave me his code, and it worked flawlessly. Maybe you, or anyone else have idea how to find position of intersection in world? It returns number of face on the mesh, where intersection happened, and U V points, not sure what are those.

Share this post


Link to post
Share on other sites
It is the inverse of the view matrix you need but the main area where people fall down with picking is to not transform the ray into the model space of the model being tested against. I have not read all your code but that may be where you are going wrong. There are some notes on this here: although the site seems to be a bit slow at the moment.

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