Sphere's Intersect method
public boolean intersect(Ray ray) {
float dx = center.x - ray.origin.x;
float dy = center.y - ray.origin.y;
float dz = center.z - ray.origin.z;
float v = ray.direction.dot(dx, dy, dz);
// Do the following quick check to see if there is even a chance
// that an intersection here might be closer than a previous one
if (v - radius > ray.t) return false;
// Test if the ray actually intersects the sphere
float t = radSqr + v*v - dx*dx - dy*dy - dz*dz;
if (t < 0) return false;
// Test if the intersection is in the positive
// ray direction and it is the closest so far
t = v - ((float) Math.sqrt(t));
if ((t > ray.t) || (t < 0))
return false;
ray.t = t;
ray.object = this;
return true;
}
|