title: How to get the intersection point? Ray Triangle Intersection C++
body: <p>I would like to get the intersection point on a triangle when a ray go through it. I followed the on line tutorial and made a function, but I could not get the correct coordinates of intersection point. </p>
  
  <p>For example, I should get the intersection point (0, 3, -1), if I use ray origin (0, 2, -1), ray direction (0, 1, 0), triangle vertices p0 (0, 3, 0), p1 (-0.5, 3, -1), p2 (0.5, 3, -1). However, I have got the intersection point (0, 7, -1), which was not correct. </p>
  
  <p>I appreciate your time and help. :-)</p>
  
  <pre><code>float kEpsilon = 0.000001;
  
  V3f crossProduct(V3f point1, V3f point2){
  
    V3f vector; 
  
    vector.x = point1.y * point2.z - point2.y * point1.z; 
    vector.y = point2.x * point1.z - point1.x * point2.z; 
    vector.z = point1.x * point2.y - point1.y * point2.x; 
  
    return vector;
  }
  
  float dotProduct(V3f dot1, V3f dot2){
  
    float dot = dot1.x * dot2.x + dot1.y * dot2.y + dot1.z * dot2.z; 
  
    return dot;
  }
  
  //orig: ray origin, dir: ray direction, Triangle vertices: p0, p1, p2.  
  bool rayTriangleIntersect(V3f orig, V3f dir, V3f p0, V3f p1, V3f p2){ 
  
  // compute plane's normal
  
    V3f p0p1, p0p2;
  
    p0p1.x = p1.x - p0.x; 
    p0p1.y = p1.y - p0.y; 
    p0p1.z = p1.z - p0.z; 
  
    p0p2.x = p2.x - p0.x;
    p0p2.y = p2.y - p0.y; 
    p0p2.z = p2.z - p0.z;
  
    // no need to normalize
    V3f N = crossProduct(p0p1, p0p2); // N 
  
    // Step 1: finding P
  
    // check if ray and plane are parallel ?
    float NdotRayDirection = dotProduct(N, dir); // if the result is 0, the function will return the value false (no intersection).
  
    if (fabs(NdotRayDirection) &lt; kEpsilon){ // almost 0 
  
        return false; // they are parallel so they don't intersect ! 
    }
  
    // compute d parameter using equation 2
    float d = dotProduct(N, p0); 
  
    // compute t (equation P=O+tR P intersection point ray origin O and its direction R)
  
    float t = (dotProduct(N, orig) + d) / NdotRayDirection; 
  
    // check if the triangle is in behind the ray
    //if (t &lt; 0){ return false; } // the triangle is behind 
  
    // compute the intersection point using equation
    V3f P; 
  
    //this part should do the work, but it does not work.
    P.x = orig.x + t * dir.x; 
    P.y = orig.y + t * dir.y; 
    P.z = orig.z + t * dir.z; 
  
  
    // Step 2: inside-outside test
    V3f C; // vector perpendicular to triangle's plane 
  
    // edge 0
    V3f edge0; 
  
    edge0.x = p1.x - p0.x;
    edge0.y = p1.y - p0.y;
    edge0.z = p1.z - p0.z;
  
    V3f vp0; 
  
    vp0.x = P.x - p0.x;
    vp0.y = P.y - p0.y; 
    vp0.z = P.z - p0.z; 
  
    C = crossProduct(edge0, vp0); 
  
    if (dotProduct(N, C) &lt; 0) { return false; }// P is on the right side 
  
    // edge 1
    V3f edge1;
  
    edge1.x = p2.x - p1.x;
    edge1.y = p2.y - p1.y;
    edge1.z = p2.z - p1.z;
  
    V3f vp1; 
  
    vp1.x = P.x - p1.x; 
    vp1.y = P.y - p1.y; 
    vp1.z = P.z - p1.z; 
  
    C = crossProduct(edge1, vp1); 
  
    if (dotProduct(N, C) &lt; 0) { return false; } // P is on the right side 
  
    // edge 2
    V3f edge2;
  
    edge2.x = p0.x - p2.x;    
    edge2.y = p0.y - p2.y;
    edge2.z = p0.z - p2.z;
  
    V3f vp2; 
  
    vp2.x = P.x - p2.x;
    vp2.y = P.y - p2.y;
    vp2.z = P.z - p2.z;
  
    C = crossProduct(edge2, vp2);
  
    if (dotProduct(N, C) &lt; 0) { return false; } // P is on the right side; 
  
    return true; // this ray hits the triangle 
  } 
  </code></pre>
  
  <p>Thanks for your help.</p>