/** Intersects this ray with a sphere. */ public Collection<Vec3f> intersect(Sphere sphere) { float[] t = Line.intersectLengths(p, d, sphere); if (t == null) return null; ArrayList<Vec3f> intersections = new ArrayList<Vec3f>(2); for (float time : t) if (time >= 0) intersections.add(p.plus(d.times(time))); return intersections; }
/** Intersects this ray with a convex polygon. */ public Vec3f intersect(ConvexPolygon poly) { float nDotD = poly.n.dot(d); if (nDotD == 0) return null; float t = poly.n.dot(poly.v[0].minus(p)) / nDotD; Vec3f x = p.plus(d.times(t)); return (poly.contains(x) ? x : null); // Vec3f x = intersect(new Plane(poly.v[0], poly.n)); // return (x != null || poly.contains(x)) ? x : null; }
/** Intersects this ray with a plane. */ public Vec3f intersect(Plane plane) { float t = Line.intersectLength(p, d, plane); return t < 0 ? null : p.plus(d.times(t)); }