Sign in to follow this  
Anddos

Building a ray from a moving position, (D3DXIntersect)

Recommended Posts

Anddos    588

I am trying to do a ray test on a mesh, its not a pick type test with mouse but a ray test where the rays position is circling the model its testing against, here is my code please tell me what I am doing wrong because its not working...

Ive built this code from a mouse ray pick and thought this is how it probably works with a ray in worldcords...

Thanks if you can help

 

 

 

Building the rays position

 
D3DXMATRIX matRotateY2,matTrans;
 
static float index2 = 0.0f; index2+=0.00003f;
D3DXMatrixTranslation(&matTrans,0.0f,0.0f,-5.0f);
D3DXMatrixRotationY(&matRotateY2, index2);
d3ddev->SetTransform(D3DTS_WORLD, &(matTrans*matRotateY2));
D3DXVECTOR3 Dst;
D3DXVec3TransformCoord(&Dst,NULL,&(matTrans*matRotateY2));
//now the circling motion is stored in a D3DXVECTOR3

Creating the ray test function

void WorldRay(LPD3DXMESH mesh,D3DXVECTOR3 Pos,D3DXMATRIX matWorldParam,D3DXMATRIX matViewParam)
{

//postion in worldspace,we set not translation

D3DXVECTOR3 teapotpos(0.0f,0.0f,0.0f); 
 
D3DXMATRIX matInverse;
 
 
 
// find the inverse matrix which takes us to localspace
D3DXMatrixInverse(&matInverse, NULL, &(matWorldParam * matViewParam));
D3DXVECTOR3 direction;
 
 
//find the direction we need to subract the 2 vectors
direction = teapotpos - Pos;
D3DXVec3Normalize(&direction,&direction);
 
 
// convert position into local space
D3DXVec3TransformCoord(&Pos, &Pos, &matInverse); 
 
// convert direction to localspace
D3DXVec3TransformNormal(&direction, &direction, &matInverse); 
 
D3DXVec3Normalize(&direction, &direction);
 
 
// detect if the ray hits the model
BOOL hit;
DWORD faceIndex;
D3DXIntersect(mesh, &Pos, &direction, &hit, &faceIndex, NULL, NULL, NULL, NULL, NULL);
 
 
if(hit)
{
d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
 
 
//drawRayTri(mesh,faceIndex);
}
 
 
else
{
d3ddev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
}
}
 

Drawing the model and calling the ray function in Begin Scene

 
// set the world transform
D3DXMATRIX matRotateY,matScale;
D3DXMatrixRotationY(&matRotateY, index);
D3DXMatrixScaling(&matScale,2.0f,2.0f,2.0f);
d3ddev->SetTransform(D3DTS_WORLD, &(matScale*matRotateY));
 
 
// draw the teapot
meshTeapot->DrawSubset(0); 
WorldRay(meshTeapot,Dst,matScale*matRotateY,matView);
 
Edited by Anddos

Share this post


Link to post
Share on other sites
Anddos    588

Never mind i figured it out, you dont have to convert the ray to local space if you are not using picking :)

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