Ray - 3d triangle intersection test

I have written simple ray-triangle intersection code based on this two tutorials: http://graphics.stanford.edu/courses/cs348b-98/gg/intersect.html, http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-9-ray-triangle-intersection/ray-triangle-intersection-geometric-solution/. It works perfectly for triangles with all points y=0, but when any (or all) points have another y values then it does not work properly.

Here is the code for intersection (glColor calls added for debugging): http://www.java-gaming.org/?action=pastebin&id=676

Triangle:

package intersection;

import org.lwjgl.util.vector.Vector3f;

public class Triangle {

    public Vector3f p0;
    public Vector3f p1;
    public Vector3f p2;
    
    public Triangle(Vector3f p0, Vector3f p1, Vector3f p2) {
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
    }
    
}

Ray:

package intersection;

import org.lwjgl.util.vector.Vector3f;

public class Ray {

    private Vector3f start;
    private Vector3f dir;
    private float angleX;
    private float angleY;
    
    public Ray(float x, float y, float z, float angleX, float angleY) {
        start = new Vector3f(x, y, z);
        dir = new Vector3f();
        this.angleX = angleX;
        this.angleY = angleY;
        updateDir();
    }
    
    Vector3f getStartVector() {
        return start;
    }
    
    public float getStartX() {
        return start.x;
    }
    
    public float getStartY() {
        return start.y;
    }
    
    public float getStartZ() {
        return start.z;
    }
    
    Vector3f getDirVector() {
        return dir;
    }
    
    public float getDirX() {
        return dir.x;
    }
    
    public float getDirY() {
        return dir.y;
    }
    
    public float getDirZ() {
        return dir.z;
    }
    
    public float getAngleX() {
        return angleX;
    }
    
    public float getAngleY() {
        return angleY;
    }
    
    public void setPosition(float x, float y, float z) {
        start.x = x;
        start.y = y;
        start.z = z;
        updateDir();
    }
    
    public void setAngle(float angleX, float angleY) {
        this.angleX = angleX;
        this.angleY = angleY;
        updateDir();
    }
    
    public void setPositionAndAngle(float x, float y, float z, float angleX, float angleY) {
        start.x = x;
        start.y = y;
        start.z = z;
        this.angleX = angleX;
        this.angleY = angleY;
        updateDir();
    }
    
    private void updateDir() {
        dir.x = (float)(Math.cos(angleY)*Math.sin(angleX));
        dir.y = (float)-Math.cos(angleX);
        dir.z = (float)(Math.sin(angleY)*Math.sin(angleX));
    }
    
}

Why this code works incorrectly? I am trying to find bug, but don’t found everything since 2 days. :-\

I’m still shaky on vectors so I didnt feel confident trying to find a mistake in your code.

I thought up another way to do it, by using 2D ray-plane intersection test 3x so that it would work for a 3D object

If you arent interested in another way to do it, i dont blame you, your way seems a lot more “correct” than mine and is probably more efficient, but anyway heres how i would do it

{
a triangle is made up of 3 points with x,y,z coordinates. a ray is just a line that is infinite in one direction.

first, i would take the x,y components of every one of the points in the triangle and break the triangle up into three lines. check if the ray intersects any of these 3 lines (once it intersects one move on to the next step ). In other words imagine flattening out the 3D object into 2D. If the line intersects it in 3D it must intersect it in 2D as well no matter which way you look at it.

second, take the y,z components of the lines of the triangles and ray, do it again.

third, x,z check intersections again.

if it intersects on every plane then the ray intersects the triangle. Again im not saying this way is “fast” or “correct”. Its just the way i would do it. shouldn’t kill performance too bad unless you are checking a ton of triangles.

You need to figure out an algorithm to see if two lines intersect. There are many, google is your friend.

there are also many algorithms that can quickly check if the ray even has a chance of intersecting the triangle.

Hope that wasn’t too stupid an algorithm, I made that up off the top of my head when i read the title of this thread, though it will work i don’t know if it’ll be too fast.
}

Good Luck!