Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / Game Design & Programming / Mouse Ray Picking Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Fengalon

April 07, 2005, 05:38 PM

Can anyone send me an example of Mouse Ray Picking technique?

 
Jason Zelsnack

April 07, 2005, 07:35 PM

Do you want a fully working program doing screen-space raycasting or just some code showing how to do it?

If you just want some code snippets I could put some up for you. The trickiest part is creating a ray from a screenspace coordinate. I use the worldSpace camera matrix and the FOV to calculate my rays instead of the view matrix like in that Brian Hook article. I find that easier to understand.

 
Fengalon

April 08, 2005, 02:26 AM

A fully working program would be best... ^^

But some code snippets would do... Thanks!

 
Illco

April 08, 2005, 01:32 PM

I don't know which platform you are working on - if any - but the DirectX SDK contains a quite clear picking sample.

Greetz,

Illco

 
Illco

April 08, 2005, 01:33 PM

Ah what the heck; here is the important part:

  1.  
  2.     HRESULT hr;
  3.     D3DXVECTOR3 vPickRayDir;
  4.     D3DXVECTOR3 vPickRayOrig;
  5.  
  6.     m_dwNumIntersections = 0L;
  7.  
  8.     // Get the pick ray from the mouse position
  9.     if( GetCapture() )
  10.     {
  11.         D3DXMATRIXA16 matProj;
  12.         m_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );
  13.  
  14.         POINT ptCursor;
  15.         GetCursorPos( &ptCursor );
  16.         ScreenToClient( m_hWnd, &ptCursor );
  17.  
  18.         // Compute the vector of the pick ray in screen space
  19.         D3DXVECTOR3 v;
  20.         v.x =  ( ( ( 2.0f * ptCursor.x ) / m_d3dsdBackBuffer.Width  ) - 1 ) / matProj._11;
  21.         v.y = -( ( ( 2.0f * ptCursor.y ) / m_d3dsdBackBuffer.Height ) - 1 ) / matProj._22;
  22.         v.z =  1.0f;
  23.  
  24.         // Get the inverse of the composite view and world matrix
  25.         D3DXMATRIXA16 matView, matWorld, m;
  26.         m_pd3dDevice->GetTransform( D3DTS_VIEW, &matView );
  27.         m_pd3dDevice->GetTransform( D3DTS_WORLD, &matWorld );
  28.        
  29.         m = matWorld * matView;
  30.         D3DXMatrixInverse( &m, NULL, &m );
  31.  
  32.         // Transform the screen space pick ray into 3D space
  33.         vPickRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
  34.         vPickRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
  35.         vPickRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
  36.         vPickRayOrig.x = m._41;
  37.         vPickRayOrig.y = m._42;
  38.         vPickRayOrig.z = m._43;
  39.     }
  40.  

 
Fengalon

April 09, 2005, 11:31 AM

I'm using Opengl/SDL on windows... I'm still not quite familiar with DirectX though...

 
Fengalon

April 09, 2005, 11:32 AM

I'm using Opengl/SDL on windows... I'm still not quite familiar with DirectX though...

 
This thread contains 7 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.