# Directx 10 3D Objects to screen coords.

This topic is 2497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello,

I would like to program to use the cursor to click on a 3D object and know that It clicked on it..Im using DirectX 10.. Is there a DX function to do this ? I have not had much luck searching for this on the web.. It might be I cant describe what Im trying ot do in the search.

I can find where my cursor is no problem as I use my own class with a quad for my cursor.. just need to figure out 3D objects to screen coord I guess. A nudge in the right direction will be greatly appeciated.. Im using C++
Thanks

##### Share on other sites
Hi,

I think what you are looking for is called picking. There is an example on the Directx SDK called Pick10 in C++

Hope that helps.
Good luck with the program ;)

##### Share on other sites
If you haven't found what your looking for yet, you might want to use the intersect method of the mesh interface (ID3DX10Mesh->Intersect()).

First you will need to transform the screen space (x,y) coordinate that you picked with the mouse to Projection space by doing something like this:

D3DXVECTOR3 pickRayProjectionSpace; pickRayProjectionSpace.x = ((( 2.0f * mouseX) / ClientWidth ) - 1 ) / Projection(0,0); pickRayProjectionSpace.y = -((( 2.0f * mouseY) / ClientHeight) - 1 ) / Projection(1,1); pickRayProjectionSpace.z = 1.0f; D3DXVECTOR3 pickRayProjectionSpacePos(0.0f, 0.0f, 0.0f); D3DXVECTOR3 pickRayProjectionSpaceDir(pickRayProjectionSpace.x, pickRayProjectionSpace.y, pickRayProjectionSpace.z);

Now that you have a 3d pick ray in projection space, you'll need to get it into world space by transforming it with the inverse of the view space matrix. something like this:

D3DXMATRIX pickRayWorldSpace; // This will hold the inverse of the view matrix (which is the world space matrix (the cameras position and orientation and stuff in world space)) D3DXMatrixInverse(&pickRayWorldSpace, 0, &View); // Your view space matrix (basically your camera's matrix) // Pick ray in world space D3DXVec3TransformCoord(&pickRayProjectionSpacePos, &pickRayProjectionSpacePos, &pickRayProjectionSpacePos); D3DXVec3TransformNormal(&pickRayProjectionSpaceDir, &pickRayProjectionSpaceDir, &pickRayProjectionSpaceDir);

Now that we have the pick ray in world space, we need to transform it into each objects space, by looping through each object we want to check for an intersection with the pick ray, and by multiplying the pick ray by the inverse of each objects world space matrix. All the above stuff only needs to be done once per click, but the next stuff needs to be done once for every object you want to check for picking with.

D3DXMATRIX pickRayLocalSpace; // ObjectLocalSpace is a matrix that defines the object position, orientation and size in world space. is because it's an array of matrices so that you can easily loop through all the world matrices of each object in the scene D3DXMatrixInverse(&pickRayLocalSpace,NULL,&ObjectLocalSpace); D3DXVECTOR3 pickRayLocalSpacePos,pickRayLocalSpaceDir; // Transform the world space pick ray into the objects space D3DXVec3TransformCoord(&pickRayLocalSpacePos,&pickRayProjectionSpacePos,&pickRayLocalSpace); D3DXVec3TransformNormal(&pickRayLocalSpaceDir,&pickRayProjectionSpaceDir,&pickRayLocalSpace); D3DXVec3Normalize(&pickRayLocalSpaceDir,&pickRayLocalSpaceDir);

Now that we have the pick ray in object space, you need to check to see if the ray intersected with the mesh. To do this, you can use an handy function (if you are using the ID3DXMesh interface for the objects in your scene):

UINT numHits = 0; float hitDistance; meshes[1]->Intersect(&pickRayLocalSpacePos, &pickRayLocalSpaceDir, &numHits, 0, 0, 0, &hitDistance, 0);

numHits is the number of times the object was intersected by the ray. if this is "0" (zero), then the ray missed the object. hitDistance is the distance from the origin of the ray (the camera's position in this case) to the first intersection with the object.

I took this information pretty much straight from my lesson on picking in direct3d 10:
http://www.braynzars...x.php?p=Picking

##### Share on other sites
Thank you very much.. This will definitly get me in the right direction..

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013708
×