C++, DirectX9, mouse position to ray?

Started by
4 comments, last by Ripiz 14 years, 7 months ago
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]
Advertisement
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.
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: <a href="http://www.toymaker.info/Games/html/picking.html",Picking&lt;/a> although the site seems to be a bit slow at the moment.
------------------------See my games programming site at: www.toymaker.info
There are builtin functions in DirectX/XNA for that purpose, use them :)
Here:
Device->(Project / Unproject).
Trip99, I used that tutorial in the begin, and it didn't work. However it's fine now, got help from other place

This topic is closed to new replies.

Advertisement