Jump to content
  • Advertisement
Sign in to follow this  

problem with picking using D3DXIntersect

This topic is 2924 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

I have recently been trying to do picking using D3DXIntersect but my code doesn't seem to quite work and I what I mean by that is that when I try to click on a mesh while using my pick function it will only recognized a hit on the mesh only a very small and specific area
also sometimes when I click near the mesh and not really on it will recognized a hit

here is my code :

HRESULT CGameWin::pick()
POINT ptCursor;
const D3DXMATRIX* pmatProj = &m_pCamera->GetProjMatrix();

GetCursorPos( &ptCursor );
ScreenToClient( m_hWnd, &ptCursor );

// Compute the vector of the Pick ray in screen space
v.x = ( ( ( 2.0f * ptCursor.x ) / m_pCamera->GetViewport().Width )-1 ) / pmatProj->_11;
v.y = -( ( ( 2.0f * ptCursor.y ) / m_pCamera->GetViewport().Height)- 1) / pmatProj->_22;
v.z = 1.0f;

D3DXMATRIX m,view;
D3DXVECTOR3 rayOrigin,rayDir;
float minDistance = 30000.0;

view = m_pCamera->GetViewMatrix();
D3DXMatrixInverse( &m, NULL, &view );

// Transform the screen space pick ray into 3D space
rayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
rayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32;
rayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33;
rayOrigin.x = m._41;
rayOrigin.y = m._42;
rayOrigin.z = m._43;

//getting our mesh object
CMyMesh* curMesh = Objects[5].m_pMesh;
// Use inverse of matrix
D3DXMATRIX matInverse,temp;
temp = Objects[5].getWorldMatrix();

// Transform ray origin and direction by inv matrix
D3DXVECTOR3 rayObjOrigin,rayObjDirection;


BOOL hasHit;
float distanceToCollision;

hRet = D3DXIntersect(curMesh->GetMesh(), &rayObjOrigin, &rayObjDirection, &hasHit, NULL, NULL, NULL, &distanceToCollision, NULL, NULL);

if (FAILED(hRet) ) return hRet;

if (hasHit)
return S_OK;
return S_FALSE;

return S_OK;

hope you can help find out what am I doing wrong here

Share this post

Link to post
Share on other sites
Your problem happens on this

When you want to transform a normal, not a point, you should pass the transpose
of the inverse of the matrix to this function. This request can be found in the remarks of this function's specification in the SDK doc.

If you want to figure out why we should do like this, you can take the "Real-time Rendering(3rd edition)->4.1.7 Normal transform" as a reference.

Share this post

Link to post
Share on other sites
thanks for the help but it appears that wasn't the only thing making my pick function to not work.
it seems that I have called D3DXMatrixPerspectiveFovLH outside my camera class so when I used the projection matrix from the camera class it cuase it to not work so I fixed it and now it is working like it should.
also thanks Real-time Rendering(3rd edition) reference I will take a look at that should help make things clearer

but now I have another question how to I make my pick function to keep working and recognizing correcty a hit on the mesh after I rotate it ?

Share this post

Link to post
Share on other sites
In my opnion you can use D3DXMatrixRotation() to rotate matInverse while your mesh is rotating.

//MATRIX matInverse
D3DXMatrixRotation(float x,float y,float z,&matInverse);

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!