#### Archived

This topic is now archived and is closed to further replies.

# a 2D click in 3D world coordinate problem

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

## Recommended Posts

a 2D click in 3D world coordinate problem lets say you have a near of 1.0f; you have a far of 1500.0f; and a view D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3( 0.0f, 0.0f,-600.0f) &D3DXVECTOR3( 0.0f, 0.0f, 0.0f) &D3DXVECTOR3( 0.0f, 1.0f, 0.0f)); g_d3d_device->SetTransform(D3DTS_VIEW,&matView); you put an object on the screen at x = 120.0f, y = -50.0f,z=0.0f ok thats easy to make a mouse pick system. but what if x = 120.0f, y = -50.0f,z=300.0f now x and y are not the same on the screen which is what you what its 3d after all. What is the math formula to find the new coordinate of x and y on the screen? seeing how your mointer is not 3d neither is your mouse. any comment would be greatly appreciated. Thx [edited by - Eric V on July 15, 2002 2:25:44 PM] [edited by - Eric V on July 15, 2002 2:27:40 PM]

##### Share on other sites
I think the SDK had a demo that did extactly this. However it''s really just a matter of projecting each 3D vertex to 2D screen space. Then when the mouse is clicked you search through the list using the most efficient algorithm available and find out which polygon was clicked (that is which 2D-enclosed-vertex-space) was clicked; of course with depth-overlap you''ll want to choose the polygon face that is closest to the viewer. All you want to do is find the 2D polygon that the mouse is within. It shouldn''t be too difficult (of course I''ve never tried it so I really don''t have a clue but it''s fun to say... )

Still, I''d look at the source of the demos that came with version 8.0 and later of the DirectX SDK. I''m almost positive you''ll find out how to do this.

##### Share on other sites
if you don''t have many objects on the screen you could use the stencil buffer to write an id number for the object being drawn, and whenever you click read the buffer and select the object with that id

another option (in OpenGL at least) is to use SELECT mode. i.e. when the click occurs set a picking matrix that clips to a small rectangle around where the user clicked and rerender in SELECT mode. OpenGL will the tell you what got rendered in that area. no idea what the d3d equivalent is though

1. 1
2. 2
3. 3
4. 4
Rutin
14
5. 5

• 10
• 11
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632652
• Total Posts
3007661
• ### Who's Online (See full list)

There are no registered users currently online

×