By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

C++ directx picking is not working

P: 14
i got help writing my picking function... but it still doesn't work im not sure if it is because the my function or because of my mesh it is one of them. someone suggest me to draw the ray, i drew my rays it basically followed my cursor(mouse).
here is my code

Expand|Select|Wrap|Line Numbers
  1.     BOOL D3dDevice::Picking(HWND hWnd, LPDIRECT3DDEVICE9 d3ddev, CXFileEntity *entity, int z)
  2.     {
  3.         D3DXVECTOR3 v;
  4.         D3DXMATRIX matProj;
  5.         POINT pt;
  6.         D3DVIEWPORT9 vp;
  7.         D3DXMATRIX matInverse, matWorld;
  8.         D3DXMATRIX m;
  9.         D3DXVECTOR3 rayOrigin,rayDir;
  10.         D3DXMATRIX matView;
  11.         D3DXVECTOR3 rayObjOrigin,rayObjDirection, rayDirection;
  12.  
  13.         GetCursorPos(&pt);
  14.         ScreenToClient(hWnd, &pt);
  15.         d3ddev->GetTransform(D3DTS_PROJECTION, &matProj);
  16.         d3ddev->GetViewport(&vp);
  17.  
  18.         d3ddev->GetTransform(D3DTS_VIEW, &matView);
  19.  
  20.         // Use inverse of matrix
  21.         d3ddev->GetTransform(D3DTS_WORLD, &matWorld);
  22.         D3DXVECTOR3 vec3( pt.x, pt.y, 1.0f );
  23.         D3DXVec3Unproject( &rayObjSpace, &vec3, &vp, &matProj, &matView, &matWorld );
  24.         // Transform ray origin and direction by inv matrix
  25.  
  26.         D3DXMATRIX invWorld;
  27.         D3DXMatrixInverse( &invWorld, NULL, &matWorld );
  28.  
  29.         D3DXVECTOR3 camObjSpace;
  30.         camPos.x=0.0;
  31.         camPos.y=0.0;
  32.         camPos.z=z;
  33.         D3DXVec3TransformCoord( &camObjSpace, &camPos, &invWorld ); 
  34.  
  35.         rayDir = rayObjSpace - camObjSpace;
  36.  
  37.         BOOL hasHit;
  38.         float distanceToCollision;
  39.  
  40.         if(FAILED(D3DXIntersect(entity->pDrawMesh, &rayObjSpace, &rayDir, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL)))
  41.         {
  42.             PostQuitMessage(0);
  43.         };
  44.  
  45.         if(hasHit==1)
  46.         {
  47.             PostQuitMessage(0);
  48.         }
  49.  
  50.         return hasHit;
  51.     }
i have no idea why it is not working, by the way i did a that `rayObjSpace.z` never equals to 0, it is always in the 80s. entity->pDrawMesh is not `LPD3DXMESH` not `LPD3DXBASEMESH`.
thanks in advance
Nov 23 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.