Ray Intersecting Plane Formula in C++/DirectX

Posted by user4585 on Game Development See other posts from Game Development or by user4585
Published on 2011-01-12T04:59:47Z Indexed on 2011/01/12 5:58 UTC
Read the original article Hit count: 290

Filed under:

I'm developing a picking system that will use rays that intersect volumes and I'm having trouble with ray intersection versus a plane. I was able to figure out spheres fairly easily, but planes are giving me trouble. I've tried to understand various sources and get hung up on some of the variables used within their explanations.

Here is a snippet of my code:

   bool Picking()
 {
  D3DXVECTOR3 vec;
  D3DXVECTOR3 vRayDir;
  D3DXVECTOR3 vRayOrig;
  D3DXVECTOR3 vROO, vROD; // vect ray obj orig, vec ray obj dir
  D3DXMATRIX m;
  D3DXMATRIX mInverse;
  D3DXMATRIX worldMat;

                // Obtain project matrix
  D3DXMATRIX pMatProj =   CDirectXRenderer::GetInstance()->Director()->Proj();
                // Obtain mouse position
  D3DXVECTOR3 pos = CGUIManager::GetInstance()->GUIObjectList.front().pos;

                // Get window width & height
  float w = CDirectXRenderer::GetInstance()->GetWidth();
  float h = CDirectXRenderer::GetInstance()->GetHeight();

                // Transform vector from screen to 3D space
  vec.x =  (((2.0f * pos.x) / w) - 1.0f) / pMatProj._11;
  vec.y = -(((2.0f * pos.y) / h) - 1.0f) / pMatProj._22;
  vec.z = 1.0f;

                // Create a view inverse matrix
  D3DXMatrixInverse(&m, NULL, &CDirectXRenderer::GetInstance()->Director()->View());

                // Determine our ray's direction
  vRayDir.x = vec.x * m._11 + vec.y * m._21 + vec.z * m._31;
  vRayDir.y = vec.x * m._12 + vec.y * m._22 + vec.z * m._32;
  vRayDir.z = vec.x * m._13 + vec.y * m._23 + vec.z * m._33;

                // Determine our ray's origin
  vRayOrig.x = m._41;
  vRayOrig.y = m._42;
  vRayOrig.z = m._43;


  D3DXMatrixIdentity(&worldMat);
  //worldMat = aliveActors[0]->GetTrans();
  D3DXMatrixInverse(&mInverse, NULL, &worldMat); 

  D3DXVec3TransformCoord(&vROO, &vRayOrig, &mInverse);
  D3DXVec3TransformNormal(&vROD, &vRayDir, &mInverse);
  D3DXVec3Normalize(&vROD, &vROD);

When using this code I'm able to detect a ray intersection via a sphere, but I have questions when determining an intersection via a plane. First off should I be using my vRayOrig & vRayDir variables for the plane intersection tests or should I be using the new vectors that are created for use in object space?

When looking at a site like this for example: http://www.tar.hu/gamealgorithms/ch22lev1sec2.html

I'm curious as to what D is in the equation AX + BY + CZ + D = 0 and how does it factor in to determining a plane intersection?

Any help will be appreciated, thanks.

© Game Development or respective owner

Related posts about c++