Advertisement Jump to content
Sign in to follow this  
Ripiz

C++, DirectX9, mouse position to ray?

This topic is 3417 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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.

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
Trip99, I used that tutorial in the begin, and it didn't work. However it's fine now, got help from other place

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!