0

Could someone please link me an algorithm on how to raycast in java lwjgl. I have a Vector3f as the origin, a Vector3f as a raycast vector, I have 3 Vector3fs for triangle points all as inputs for my desired function.

Currently I have,

public static boolean rayIntersectsTriangle(Vector3f rayOrigin, 
                                                Vector3f rayVector,
                                                Vector3f triangleP0,
                                                Vector3f triangleP1,
                                                Vector3f triangleP2,
                                                Vector3f outIntersectionPoint) {
        Vector3f vertex0 = triangleP0;
        Vector3f vertex1 = triangleP1;
        Vector3f vertex2 = triangleP2;
        Vector3f edge1 = new Vector3f();
        Vector3f edge2 = new Vector3f();
        Vector3f h = new Vector3f();
        Vector3f s = new Vector3f();
        Vector3f q = new Vector3f();
        double a, f, u, v;
        Vector3f.sub(vertex1, vertex0, edge1);
        Vector3f.sub(vertex2, vertex0, edge2);
        Vector3f.cross(rayVector, edge2, h);
        a = (float)Vector3f.dot(edge1, h);
        if (a > -EPSILON && a < EPSILON) {
            return false;    // This ray is parallel to this triangle.
        }
        f = 1.0 / a;
        Vector3f.sub(rayOrigin, vertex0, s);
        u = f * (Vector3f.dot(s, h));
        if (u < 0.0 || u > 1.0) {
            return false;
        }
        Vector3f.cross(s, edge1, q);
        v = f * Vector3f.dot(rayVector, q);
        if (v < 0.0 || u + v > 1.0) {
            return false;
        }
        // At this stage we can compute t to find out where the intersection point is on the line.
        double t = f * Vector3f.dot(edge2, q);
        if (t > EPSILON) // ray intersection
        {
            outIntersectionPoint = new Vector3f(0, 0, 0);
            outIntersectionPoint = Vector3fMath.Vector3fLerp(rayOrigin, rayVector, (float)t);
            return true;
        } else // This means that there is a line intersection but not a ray intersection.
        {
            return false;
        }
    }

which I tried converting to lwjgl code from https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm

Thanks

James G
  • 1
  • 2

0 Answers0