Sign in to follow this  
XDarkAngelX

problem with picking using D3DXIntersect

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
D3DXVECTOR3 v;
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;
HRESULT hRet;

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();
D3DXMatrixInverse(&matInverse,NULL,&temp);

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

D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse);
D3DXVec3Normalize(&rayObjDirection,&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;
else
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
...
D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse);
...

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

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