Browse Source

Discard Ray::HitDistance() result to a triangle behind the ray. Fixes #437.

Lasse Öörni 11 years ago
parent
commit
cc98bb9199
1 changed files with 10 additions and 5 deletions
  1. 10 5
      Source/Engine/Math/Ray.cpp

+ 10 - 5
Source/Engine/Math/Ray.cpp

@@ -229,11 +229,16 @@ float Ray::HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2,
             float v = direction_.DotProduct(q);
             if (v >= 0.0f && u + v <= det)
             {
-                // There is an intersection, so calculate distance & optional normal
-                if (outNormal)
-                    *outNormal = edge1.CrossProduct(edge2);
-                
-                return edge2.DotProduct(q) / det;
+                float distance = edge2.DotProduct(q) / det;
+                // Discard hits behind the ray
+                if (distance >= 0.0f)
+                {
+                    // There is an intersection, so calculate distance & optional normal
+                    if (outNormal)
+                        *outNormal = edge1.CrossProduct(edge2);
+                    
+                    return distance;
+                }
             }
         }
     }