Picking: Finding out if/where a mouse click lies on a 3d plane
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?
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.
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?
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?
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.
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.
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
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.
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:
This is coming out with all kinds of weird stuff, like values changing without the mouse even moving.
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.
http://www.toymaker.info/Games/html/picking.html
just an interesting tutorial i found i havent tried it out yet
just an interesting tutorial i found i havent tried it out yet
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement