• Advertisement
Sign in to follow this  

Building a ray from a moving position, (D3DXIntersect)

This topic is 1638 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 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;
D3DXMatrixRotationY(&matRotateY2, index2);
d3ddev->SetTransform(D3DTS_WORLD, &(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;
// 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);
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);
d3ddev->SetTransform(D3DTS_WORLD, &(matScale*matRotateY));
// draw the teapot
Edited by Anddos

Share this post

Link to post
Share on other sites

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
Sign in to follow this  

  • Advertisement