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. :-\