Advertisement Jump to content
Sign in to follow this  

Ray picking SOLVED

This topic is 4909 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

ASPECT = 1.333333 ANGLE = 45 sx = 2d mouse position x (int) sy = 2d mouse position y (int) the result i get only affects when i move the camera and when i move the mouse in the view nothing but the 4 last deciamls change the code is bad but its my first attempt to ray picking iknow that i use the y compnent of the camera pos and view to z becouse i change back when i get the result
FLOAT screenx,screeny;
   screenx = (float)sx;
   screeny = (float)sy;
   D3DXVECTOR3 res;
   D3DXVECTOR3 vD,vU,vV;
   FLOAT fov,comp;
   vD.x = (View.x - Pos.x);
   vD.y = (View.z - Pos.z);
   vD.z = (View.y - Pos.y);

   vU.x = Up.x;
   vU.y = Up.z;
   vU.z = Up.y;
   comp = vU.x*vD.x + vU.y*vD.y + vU.z*vD.z;
   vU -= comp * vD;
   fov = tanf(ANGLE*D3DX_PI/360.0f);
   vU *= fov;
   vV.x = vU.y*vD.z-vD.y*vU.z;  
   vV.y = vU.z*vD.x-vD.z*vU.x;
   vV.z = vU.x*vD.y-vD.x*vU.y; 
   vV *= ASPECT;
   FLOAT w,h;
   FLOAT mx,my,mz;
   w = 2.0f*screenx/800.0f-1.0f;
   h = 1.0f-2.0f*screeny/600.0f;
   mx = (vD.x+vU.x)*h+vV.x*w;
   my = (vD.y+vU.y)*h+vV.y*w;
   mz = (vD.z+vU.z)*h+vV.z*w;
   res.x = (Pos.x - Pos.y) * mx / mz;
   res.y = (Pos.z - Pos.y) * my / mz;
   res.z = 1;
   return res;

do anyone know a sulution to a better code or link me to a page thx Hunter Ex [Edited by - Hunter_Ex on August 8, 2005 4:48:01 PM]

Share this post

Link to post
Share on other sites
thx : D

how comes id miss that function : /

but know im a little lost how will the ray be??

can someone just point me in the right direction of using this function ( D3DXVec3Unproject )
so i not waste more time than already did :D

Share this post

Link to post
Share on other sites
D3DXVECTOR3 vMousePos3D;
D3DXVECTOR3 vCamPos = camera.position;
D3DVIEWPORT9 viewport;
D3DXMATRIX matView, matWorld, matProj;

mouse.x = (float)sx;
mouse.y = (float)sy;
mouse.z = 1.0f;

//Get the viewport

//Get the transform matrices
device->GetTransform(D3DTS_WORLD, &matWorld);
device->GetTransform(D3DTS_VIEW, &matView);
device->GetTransform(D3DTS_PROJECTION, &matProj);

//Unproject the mouse position from 2D screen space into 3D world space
D3DXVec3Unproject(&vMousePos3D, &vMouse, &viewport, &matProj, &matView, &matWorld);

//Subtract the 3D mouse position from the camera position to get direction and magnitude
D3DXVECTOR3 temp = vMousePos3D - vCamPos;

//Normalize the vector to get just direction
D3DXVECTOR3 vRayDirection;
D3DXVec3Normalize(&vRayDirection, &temp);

//Construct a ray that goes from your camera through the mouse which you can use for picking
Ray ret; //I'm assuming that you have a ray class with start vector and direction
ret.start = vCamPos;
ret.direction = vRaydirection;

return ret;

I'm just coding off the top of my head here, so it might have some errors in it. There's a few lines of implementation specific code and lots of room for optimizations. The biggest potential optimization is using a software copy of the 3 transforms and the viewport so you don't have to query the D3D device driver all the time.

Share this post

Link to post
Share on other sites
thx alot all i really wondered about was the viewport but know i know how to get it and yes i will use the predefined world matrices and i have an ray class ; )

thx again

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. 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!