| 215 | } |
| 216 | |
| 217 | bool IntersectRayTriangle(const Ray& ray, const Triangle& tri, float& t) { |
| 218 | constexpr float EPSILON = 1e-6f; |
| 219 | |
| 220 | // Adjust the triangle vertices by the object's position |
| 221 | FVector v0 = tri.v0; |
| 222 | FVector v1 = tri.v1; |
| 223 | FVector v2 = tri.v2; |
| 224 | |
| 225 | FVector edge1 = v1 - v0; |
| 226 | FVector edge2 = v2 - v0; |
| 227 | FVector h = ray.Direction.Cross(edge2); |
| 228 | float a = edge1.Dot(h); |
| 229 | |
| 230 | if (std::abs(a) < EPSILON) |
| 231 | return false; // Ray is parallel to triangle |
| 232 | |
| 233 | float f = 1.0f / a; |
| 234 | FVector s = ray.Origin - v0; |
| 235 | float u = f * s.Dot(h); |
| 236 | |
| 237 | if (u < 0.0f || u > 1.0f) |
| 238 | return false; |
| 239 | |
| 240 | FVector q = s.Cross(edge1); |
| 241 | float v = f * ray.Direction.Dot(q); |
| 242 | |
| 243 | if (v < 0.0f || u + v > 1.0f) |
| 244 | return false; |
| 245 | |
| 246 | t = f * edge2.Dot(q); |
| 247 | return t > EPSILON; |
| 248 | } |
| 249 | |
| 250 | bool IntersectRayTriangleAndTransform(const Ray& ray, FVector pos, const Triangle& tri, float& t) { |
| 251 | constexpr float EPSILON = 1e-6f; |
no test coverage detected