• Advertisement
Sign in to follow this  

Ray Picking

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

Hi, I have a problem, my ray picking doesn't work, here is my function
bool Map::SendRay()
{
POINT Mouse;
GetCursorPos( &Mouse );
ScreenToClient( gAppWizard.GetWindowHandle(), &Mouse );



D3DXMATRIX matProj, matView, matWorld, m, matInverse;
D3DXVECTOR3 v, rayOrigin, rayDir;

gAppWizard.GetD3DDev()->GetTransform( D3DTS_PROJECTION, &matProj );
gAppWizard.GetD3DDev()->GetTransform( D3DTS_VIEW, &matView );
gAppWizard.GetD3DDev()->GetTransform( D3DTS_WORLD, &matView );


v.x = ( ( ( 2.0f * Mouse.x ) / 800 ) - 1 ) / matProj._11;
v.y = -( ( ( 2.0f * Mouse.y ) / 600 ) - 1 ) / matProj._22;
v.z = 1.0f;

D3DXMatrixInverse( &m, NULL, &matView );

// Transform the screen space pick ray into 3D spac
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;

D3DXMatrixInverse(&matInverse,NULL,&matWorld);

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

D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse);
D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse);
D3DXVec3Normalize(&rayObjDirection,&rayObjDirection);

float distanceToCollision;

if( D3DXIntersectTri( &D3DXVECTOR3( 1.0f, 5.0f, 1.0f ), &D3DXVECTOR3( 1.0f, 5.0f, 1.0f ), &D3DXVECTOR3( 1.0f, 1.0f, 1.0f ), &rayObjOrigin, &rayObjDirection, NULL, NULL, &distanceToCollision ) )
{
return true;
}

return false;
}


This method/function always returns false, my camera is positioned at:
x = D3DXVECTOR3(0.0f,0.0f,-50.0f)
y = D3DXVECTOR3(0.0f,0.0f,0.0f)
z = D3DXVECTOR3(0.0f,1.0f,0.0f) )

Share this post


Link to post
Share on other sites
Advertisement
I dont understand.

This is how i set up my camera
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3(0.0f,0.0f,-50.0f), &D3DXVECTOR3(0.0f,0.0f,0.0f), &D3DXVECTOR3(0.0f,1.0f,0.0f) );
gAppWizard.GetD3DDev()->SetTransform( D3DTS_VIEW, &matView );

Share this post


Link to post
Share on other sites
There is at least an error in using matWorld. Extracting the relevant lines from the OP's code snippet shows this:


D3DXMATRIX matProj, matView, matWorld, m, matInverse;

gAppWizard.GetD3DDev()->GetTransform( D3DTS_VIEW, &matView ); // <-- VIEW matrix is loaded into matView
gAppWizard.GetD3DDev()->GetTransform( D3DTS_WORLD, &matView ); // <-- WORLD matrix is overriding matView (!)

D3DXMatrixInverse(&matInverse,NULL,&matWorld); // <-- matWorld is used but not correctly initialized

D3DXVec3TransformCoord(&rayObjOrigin,&rayOrigin,&matInverse); // <-- world-to-local transformation in use
D3DXVec3TransformNormal(&rayObjDirection,&rayDir,&matInverse); // <-- world-to-local transformation in use


Share this post


Link to post
Share on other sites

I dont understand.

This is how i set up my camera
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3(0.0f,0.0f,-50.0f), &D3DXVECTOR3(0.0f,0.0f,0.0f), &D3DXVECTOR3(0.0f,1.0f,0.0f) );
gAppWizard.GetD3DDev()->SetTransform( D3DTS_VIEW, &matView );


The confusion is because you stated your camera's position is described by the 3 vectors you use in the D3DXMatrixLookAt function. Only one of those vectors is the camera's position. Take a look at the documentation for D3DXMatrixLookAtLH.

Share this post


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

  • Advertisement