a 2D click in 3D world coordinate problem

Started by
1 comment, last by Eric V 21 years, 9 months ago
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]
EV
Advertisement
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.
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
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V

This topic is closed to new replies.

Advertisement