using System; using OpenTK; namespace org.niclasundharald.engine { public static class Geometry { /** * Intersect Triangle and Ray: * * A + i * (B-A) + j * (C-A) = P + n * V * A-P + i * (B-A) + j * (C-A) = n * V * i * (B-A) + j * (C-A) - n * V = P-A * * => * * -n * V + i * (B-A) + j * (C-A) = P-A * * **/ public static bool intersectTriangle(Vector3 P,Vector3 V,Vector3 A,Vector3 B,Vector3 C,out Vector3 p){ Vector3 BA,CA,PA; BA = B-A; CA = C-A; PA = P-A; Console.WriteLine("Interesction:"); Console.WriteLine("Ray: {0} + i * {1}",P,V); Console.WriteLine("Triangle: {0}",A); Console.WriteLine(" {0}",B); Console.WriteLine(" {0}",C); if (!Linear.Solve4x3(V,BA,CA,PA,out p)){ return false; } p.X = -p.X; if (p.X < 0){ return false; } if ((p.Y < 0) || (p.Y > 1.0f) || (p.Z < 0) || (p.Z > 1.0f)){ return false; } p = P + (p.X * V); return true; } } }