Jump to content
  • Advertisement

Archived

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

Mythar

Mouse in 3D

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Dose anyone have some sample code of how to determin if the mouse is pointing at a 3D object. By 3D object, I meen a list of D3DVERTEX''s. I know the function "PointInPoly" but that only checks a single poly in 2D. Is there anyway to pull the transformed data of the card ? Or transform the Mouse into 3D. If anyone has some sample code it would be great !

Share this post


Link to post
Share on other sites
Advertisement
You have to use a technique that''s called "ray casting". First, you get the 3D line that the mouse cursor''s on (it''s like "x=az+b, y=cz+d"). Then, you do some intersection tests with the polygons in the object to see if the rays intersect (if the object''s solid, you only need to test with the polygons whose normals are facing the camera, and you can reuse lighting information with that). Also, if you can access the Z-Buffer on the display card and read from it, you can check the z-buffer''s value at those mouse coordinates and only check with polygons that have points with both higher and lower Z values than that.

Share this post


Link to post
Share on other sites
Actually, you should only have to do it in 2d. Just loop through all the polygons it could possibly be on, and see which is the closest that the mouse point is inside of. It works great!

~BenDilts(LPWEBSITE www.geocities.com/benbeandogdilts);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This is how I did it:


D3DVECTOR CMyD3DApplication::MouseTo3D (int x, int y, float z)
{
double d;
D3DVECTOR tv, norm;
D3DVIEWPORT7 vp;
D3DMATRIX M, preM, invM, Proj, View, World, invProj, invView, invWorld;

m_pd3dDevice->GetViewport(&vp);
norm.x=(float)((x-vp.dwX)*2.0/vp.dwWidth)-1.0f;
norm.y=(float)((y-vp.dwY)*2.0/vp.dwHeight)-1.0f;
norm.z=0.0f;
norm.y*=-1;

if (z==0)
{
tv.x=viewx;
tv.z=viewz;
tv.y=viewy;
}

if (z==1)
{
m_pd3dDevice->GetTransform(D3DTRANSFORMSTATE_VIEW,(D3DMATRIX *)&View);
m_pd3dDevice->GetTransform(D3DTRANSFORMSTATE_PROJECTION,(D3DMATRIX *)&Proj);
m_pd3dDevice->GetTransform(D3DTRANSFORMSTATE_WORLD,(D3DMATRIX *)&World);

View._14=0;
View._24=0;
View._34=0;
View._44=1;
Proj._14=0;
Proj._24=0;
Proj._34=0;
Proj._44=1;
World._14=0;
World._24=0;
World._34=0;
World._44=1;
D3DMath_MatrixInvert(invView,View);
D3DMath_MatrixInvert(invProj,Proj);
D3DMath_MatrixInvert(invWorld,World);

D3DMath_MatrixMultiply(preM,invProj,invView);
D3DMath_MatrixMultiply(M,preM,invWorld);
D3DMath_VectorMatrixMultiply(tv,norm,M);
}

return (tv);
}


The function returns a Vector with the coordinates in the 3D space.
The parameters x/y are the screen mouse-coordinates, z is 1 (point at the near clipping plane) or 0 (the camera point).
After you''ve got the point on the near clipping plane you can make a ray-triangle intersection test for example.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!