Convert this method from ray origin and direction to just using 2 ray points, and…
- User
Convert this method from ray origin and direction to just using 2 ray points, and checking if that line intersects the quad and returning where if it does:
private static Vector3 rayQuadIntersection(Vector3 rayOrigin, Vector3 rayDirection, Vector3 v1, Vector3 v2, Vector3 v3) { // https://stackoverflow.com/questions/21114796/3d-ray-quad-intersection-test-in-java // 1. Get tht normal of the plane Vector3 a = VectorUtils.subNew(v2, v1); Vector3 b = VectorUtils.subNew(v3, v1); Vector3 normal = VectorUtils.cross(a, b); normal.normalize();
code// 2. double dotDifference = normal.dot(rayDirection); // Parallel tolerance if(Math.abs(dotDifference) < 1e-6f) return null; double t = -normal.dot(VectorUtils.subNew(rayOrigin, v1)) / dotDifference; if(t < 0) return null; // Check if the intersection is behind the rays origin? Vector3 collision = VectorUtils.addNew(rayOrigin, VectorUtils.scaleNew(rayDirection, t)); // 3. // 3. Project collision point onto the plane defined by quad vertices Vector3 dMS1 = VectorUtils.subNew(collision, v1); double dotA = a.dot(a); double dotB = b.dot(b); double u = dMS1.dot(a) / dotA; double v = dMS1.dot(b) / dotB;// // Vector3 dMS1 = VectorUtils.subNew(collision, v1); // double u = dMS1.dot(a); // double v = dMS1.dot(b);
code// 4. if(u >= 0.0f && u <= 1.0f && v >= 0.0f && v <= 1.0f) { return collision; } return null; } - Assistant