Jump to content
  • Advertisement
Sign in to follow this  
Crazy Chicken

2D Screen to 3D world projection. I am confused !!!

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

I have noticed that there are two possible ways to convert 2D screen coordinates into 3D world coordinates. The problem is that each approach gives me a different result for my ray origin and direction. Here are the two aprroaches as implemented in my code. Function 1:
void ScreenToWorld1( iX, int iY )
  D3DVIEWPORT9 viewport;
  D3DXMATRIXA16 matProj, matView, matWorld;
  D3DXVECTOR3 vRayOrigin, vRayDir;
  D3DXVECTOR3 vMouseNear, vMouseFar;
  vMouseNear = D3DXVECTOR3((float)iX, (float)iY, 0.0f);
  vMouseFar = D3DXVECTOR3((float)iX, (float)iY, 1.0f);


  m_pD3DDevice->GetTransform( D3DTS_PROJECTION, &matProj );
  m_pD3DDevice->GetTransform( D3DTS_VIEW, &matView );

  D3DXVec3Unproject( &vRayOrigin, &vMouseNear, &viewport, &matProj, &matView, &matWorld );
  D3DXVec3Unproject( &vRayDir, &vMouseFar, &viewport, &matProj, &matView, &matWorld );

void ScreenToWorld2( int iX, int iY )
  D3DVIEWPORT9 view;
  D3DXMATRIXA16 matProj, matView, matWorld, m;
  D3DXVECTOR3 vRayOrigin, vRayDir;

  m_pD3DDevice->GetTransform(D3DTS_PROJECTION, &matProj);	

  // Compute the vector of the pick ray in screen space
  v.x =  ( ( ( 2.0f * iX ) / view.Width  ) - 1 ) / matProj._11;
  v.y = -( ( ( 2.0f * iY ) / view.Height ) - 1 ) / matProj._22;
  v.z =  1.0f;

  m_pD3DDevice->GetTransform(D3DTS_VIEW, &matView);

  m = matWorld * matView;
  D3DXMatrixInverse( &m, NULL, &m );

  // Transform the screen space pick ray into 3D space
  vRayDir.x  = v.x*m._11 + v.y*m._21 + v.z*m._31;
  vRayDir.y  = v.x*m._12 + v.y*m._22 + v.z*m._32;
  vRayDir.z  = v.x*m._13 + v.y*m._23 + v.z*m._33;
  vRayOrigin.x = m._41;
  vRayOrigin.y = m._42;
  vRayOrigin.z = m._43;	

What is really confusing is that I need to use Function 1 when checking for ray-3D plane intersection and Function 2 for ray-mesh intersection. If some could please clear this up for me I would be very grateful. Thanks, Lukas

Share this post

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

  • 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!