Komak57

Collisions.cs

May 8th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1.  
  2.         public static bool LinePlane2(Vector start, Vector end, Vector[] face, Vector norm)
  3.         {
  4.             //tmp = start.Subtract(end);
  5.             Vector3 lineDirection = -end.Vec3 - -start.Vec3;//end.Subtract(start).Vec3;
  6.  
  7.             //if ((Vector3.Dot(norm.Vec3, face[0].Vec3) - Vector3.Dot(start.Vec3, norm.Vec3)) / Vector3.Dot(lineDirection, norm.Vec3) < 0) // ray collides behind direction
  8.             //return false;
  9.             //lineDirection.Y = -end.Y - -start.Y;
  10.             // Prepare our barycentric variables
  11.             // UV are our pixel coordinates in a 0 to 1 range
  12.             Vector3 u, v, triangleNormal;
  13.  
  14.             //tmp = face[1].Subtract(face[0]);
  15.             u = face[1].Subtract(face[0]).Vec3;
  16.  
  17.             //tmp = face[2].Subtract(face[0]);
  18.             v = face[2].Subtract(face[0]).Vec3;
  19.  
  20.             triangleNormal = Vector3.Cross(u, v);
  21.  
  22.             // Paranoid Check for collapsed triangle
  23.             if (triangleNormal == new Vector3(0.0f, 0.0f, 0.0f))
  24.             {
  25.                 // triangle is degenerate
  26.                 return false;
  27.             }
  28.  
  29.  
  30.             //tmp = face[0].Subtract(end);
  31.             Vector3 triangleToLineDirection = face[0].Subtract(end).Vec3;
  32.  
  33.             // Parameters for ray/plane intersection
  34.             float a, b, r;
  35.  
  36.             a = Vector3.Dot(triangleNormal, triangleToLineDirection);
  37.  
  38.             b = Vector3.Dot(triangleNormal, lineDirection);
  39.  
  40.             if (a < 0.0001f) // parallel check
  41.             {
  42.                 if (a == 0)
  43.                 {
  44.                     // ray lies in triangle plane
  45.                     return false;
  46.                 }
  47.                 else
  48.                 {
  49.                     // ray is disjointed from triangle's plane
  50.                     return false;
  51.                 }
  52.             }
  53.  
  54.             r = a / b;
  55.             if (r < 0.0f)
  56.             {
  57.                 // Line points away from triangle
  58.                 return false;
  59.             }
  60.  
  61.             // This is the intersection point of the line direction to the triangles plane
  62.             //tmp = start;
  63.             //Vector3 intersectionPoint = start.Vec3;
  64.             Vector3 intersectionPoint = Vector3.Add(start.Vec3, lineDirection * r);
  65.  
  66.             // This is crazy vector dot product math that essentially is a set of line to point
  67.             // collision checks
  68.             float uu, uv, vv, wu, wv, D;
  69.             uu = Vector3.Dot(u, u);
  70.             uv = Vector3.Dot(u, v);
  71.             vv = Vector3.Dot(v, v);
  72.  
  73.             // Now we are getting ray from the intersection point to the refrence
  74.             // point on the triangle
  75.             //tmp = face[0];
  76.             Vector3 refrencePointOnTriangle = face[0].Vec3;
  77.             Vector3 w = intersectionPoint - refrencePointOnTriangle;
  78.  
  79.             wu = Vector3.Dot(w, u);
  80.             wv = Vector3.Dot(w, v);
  81.  
  82.             D = uv * uv - uu * vv;
  83.  
  84.             // Testing the "parametric" coordinates
  85.             float s, t;
  86.             s = (uv * wv - vv * wu) / D;
  87.             if (s < 0.0 || s > 1.0)        // I is outside T
  88.             {
  89.                 // Line Intersection is outside of the Triangle
  90.                 return false;
  91.             }
  92.  
  93.             t = (uv * wu - uu * wv) / D;
  94.             if (t < 0.0 || (s + t) > 1.0)
  95.             {
  96.                 // I is outside T
  97.                 return false;
  98.             }
  99.  
  100.             // The line does intersect
  101.             return true;
  102.         }
Advertisement
Add Comment
Please, Sign In to add comment