Hi all
I searched through Xith a bit but could not find intersection tools like Ray-Triangle intersection.
If anyone need it, I made it yesterday:
class Triangle {
Point3f[] points = new Point3f[3];
public Triangle(Point3f point1, Point3f point2, Point3f point3) {
points[0] = point1;
points[1] = point2;
points[2] = point3;
}
public Point3f getPointOne() {
return points[0];
}
public Point3f getPointTwo() {
return points[1];
}
public Point3f getPointThree() {
return points[2];
}
public Point3f intersects(Point3f from, Vector3f dir) {
Ray ray = new Ray(from, dir);
return intersectRayTriangle(ray, this);
}
}
class Ray {
Point3f from = null;
Vector3f dir = null;
public Ray(Point3f from, Vector3f dir) {
this.from = from;
this.dir = dir;
}
public Point3f getStart() {
return from;
}
public Vector3f getDirection() {
return dir;
}
}
public static final float SMALL_NUM = 0.00000001f;
public Point3f intersectRayTriangle(Ray R, Triangle T) {
Point3f I = new Point3f();
Vector3f u, v, n;
Vector3f dir, w0, w;
float r, a, b;
u = new Vector3f(T.getPointTwo());
u.sub(T.getPointOne());
v = new Vector3f(T.getPointThree());
v.sub(T.getPointOne());
n = new Vector3f(); // cross product
n.cross(u, v);
if (n.length() == 0) {
return null;
}
dir = new Vector3f(R.getDirection());
w0 = new Vector3f(R.getStart());
w0.sub(T.getPointOne());
a = -(new Vector3f(n).dot(w0));
b = new Vector3f(n).dot(dir);
if ((float)Math.abs(b) < SMALL_NUM) {
return null;
}
r = a / b;
if (r < 0.0) {
return null;
}
I = new Point3f(R.getStart());
I.x += r * dir.x;
I.y += r * dir.y;
I.z += r * dir.z;
return I;
}
Alonzo

