Sign in to follow this  
mrWodoo

Ray Picking

Recommended Posts

mrWodoo    100
Hi, I have a problem, my ray picking doesn't work, here is my function
[code]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;
}[/code]

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

Share this post


Link to post
Share on other sites
mrWodoo    100
I dont understand.

This is how i set up my camera
[code] 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 );[/code]

Share this post


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

[code]
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
[/code]

Share this post


Link to post
Share on other sites
Buckeye    10747
[quote name='mrWodoo' timestamp='1307443425' post='4820460']
I dont understand.

This is how i set up my camera
[code] 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 );[/code]
[/quote]
The confusion is because you stated your camera's [i]position[/i] 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

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