I can't tell if this code wants for r1 and r2. It's for the ray, but is r1 the origin…
- User
I can't tell if this code wants for r1 and r2. It's for the ray, but is r1 the origin and r2 is the direction, or is r2 the other point of the ray? Take a look at the method body and guess/
private static Vector3 rayQuadIntersection(Vector3 r1, Vector3 r2, 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(); // 2. Vector3 difference = VectorUtils.subNew(r1, r2); double dotDifference = normal.dot(difference); // Parallel tolerance if(Math.abs(dotDifference) < 0) return null; double t = -normal.dot(VectorUtils.subNew(r1, v1)) / dotDifference; Vector3 collision = VectorUtils.addNew(r1, VectorUtils.scaleNew(difference, t)); // 3. Vector3 dMS1 = VectorUtils.subNew(collision, v1); double u = dMS1.dot(a); double v = dMS1.dot(b); // 4. if(u >= 0.0f && u <= a.dot(a) && v >= 0.0f && v <= b.dot(b)) { return collision; } return null; } - Assistant
Based on the method body you provided, it appears that
r1is used as the origin point of the ray, andr2seems to represent another point along the ray to determine its direction. This conclusion is drawn from how these parameters are utilized within the method:-
Determination of Ray Direction: The direction of the ray is calculated by the line
Vector3 difference = VectorUtils.subNew(r1, r2);, which subtractsr2fromr1. This is a bit unconventional because typically, in ray calculations, the direction vector is given directly or calculated by subtracting the origin from the direction point, i.e.,direction = point - origin. However, this code seems to treatr2as a point on the ray from which the direction (relative tor1) can be inferred by subtraction. Usually, the direction vector is normalized, but this code calculatesdifferenceand uses it for further computations without normalizing, relying instead on the calculation oftto scale this vector properly to find the intersection point. -
Error in Parallel Check: The line
if(Math.abs(dotDifference) < 0) return null;is likely a mistake. It's intended to returnnullifdotDifferenceis so small that it indicates the ray is parallel to the plane (since the dot product of the normal of the plane and the direction of the ray would be 0 if they are perpendicular). However,< 0is illogical sinceMath.abs(dotDifference)can never be negative. This should probably be a small epsilon value to account for floating-point inaccuracies, e.g.,if(Math.abs(dotDifference) < 1e-6) return null;. -
Use in Intersection Calculation: The calculation of
tand subsequentlycollisionfurther underscores thatr1is treated as the origin since it's directly used with the scaleddifferencevector to find the intersection point. This aligns with the convention of calculating a point on a ray withpoint = origin + t * direction.
In summary, based on the code,
r1is the origin of the ray, andr2is another point on the ray used to indirectly calculate the ray's direction (though this is not the typical or most clear way to represent a ray's direction). For clarity and convention, rays are usually represented by an origin point and a normalized direction vector, so the handling ofr2here is unconventional and might lead to confusion. -