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

Started by
14 comments, last by ktuluorion 18 years, 7 months ago
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?
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
Advertisement
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.
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?
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
look at gluUnProject:
http://www.ugrad.cs.ubc.ca/~cs414/opengl/gluUnProject.html

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.
I'm not really using OPENGL, so that will not help me with DirectX parameters.
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
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.
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.
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
AAAH! This is driving me crazy. It should work!
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
http://www.toymaker.info/Games/html/picking.html

just an interesting tutorial i found i havent tried it out yet
-Scotto
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)
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]

This topic is closed to new replies.

Advertisement