# Picking: Finding out if/where a mouse click lies on a 3d plane

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

## Recommended Posts

That about says it.. I want to take a mouse click, and figure out what coords the mouse is over on a 2d plane located in 3d space. Anybody have any ideas?

##### Share on other sites
hi there ktuluorion,
How are you doing?

The Problem
Transforming mouse coordinates from screen space to 3d space.

The Solution
Buddy, you might want to look into the following areas.
D3DXVec3Unproject Function
"Projects a vector from screen space into object space."
So just give it the screen coordinates of the mouse in your window. Remember that the angle at which your camera is, is also a factor and might complicate matter.

I hope this helps. Take care buddy.

##### Share on other sites
Hi, I am doing well, thank you.

I believe this function you have given me will help me. However, what I am unsure of is what I need to put into the D3DXVECTOR3 that I pass to it. I would imagine it would be X and Y coords of the mouse, but what would the third value be, since the screen is only 2d?

##### Share on other sites
look at gluUnProject:

You may have to modify the y value from gl y (bottom) to window y (top)

From the link you will see that you also need to pass GL_MODELVIEW_MATRIX, GL_PROJECTION_MATRIX, and GL_VIEWPORT values.

##### Share on other sites
I'm not really using OPENGL, so that will not help me with DirectX parameters.

##### Share on other sites
If you want a ray you will have to have 2 vectors.. one for the near plane and one for the far plane.
You would then have
[source lang = "cpp"]D3DXVECTOR3 near;near.x = mouse.X;near.y = mouse.Y;near.z = 0;D3DXVECTOR3 far;near.x = mouse.X;near.y = mouse.Y;near.z = 1;

You can then just subtract the one vector from the other. This should work
D3DXVECTOR3 ray = far - near; which would give you a ray.

You can then use the D3DXIntersect Function to check if the ray intersects with a given mesh.

##### Share on other sites
This isn't quite working out right:

D3DXVECTOR3 v,vf,vn;POINT lp;D3DVIEWPORT9 vp;GetCursorPos(&lp);D3DXMATRIX matproj;D3DXMATRIX matview;D3DXMATRIX matworld;d3ddevice->GetViewport(&vp);d3ddevice->GetTransform(D3DTS_VIEW,&matview);d3ddevice->GetTransform(D3DTS_WORLD,&matworld);d3ddevice->GetTransform(D3DTS_PROJECTION,&matproj);D3DXVec3Unproject(&vn,&D3DXVECTOR3(lp.x,lp.y,0),&vp,&matproj,&matview,&matworld);D3DXVec3Unproject(&vf,&D3DXVECTOR3(lp.x,lp.y,1),&vp,&matproj,&matview,&matworld);v=vf-vn;

This is coming out with all kinds of weird stuff, like values changing without the mouse even moving.

##### Share on other sites
AAAH! This is driving me crazy. It should work!

##### Share on other sites
http://www.toymaker.info/Games/html/picking.html

just an interesting tutorial i found i havent tried it out yet

##### Share on other sites
Does anyone see an error in my code? I can't find anything wrong with it... I zeroed out the world matrix (I realized that is why it wasn't coming out as a static number without moving) now I get the number 1 all the time as the ray's x value (or any value)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634070
• Total Posts
3015330
×