intersectSphere method
Implementation
Vector3? intersectSphere(BoundingSphere sphere, Vector3 target) {
_vector.sub2(sphere.center, origin);
final tca = _vector.dot(direction);
final d2 = _vector.dot(_vector) - tca * tca;
final radius2 = sphere.radius * sphere.radius;
if (d2 > radius2) return null;
final thc = math.sqrt(radius2 - d2);
// t0 = first intersect point - entrance on front of sphere
final t0 = tca - thc;
// t1 = second intersect point - exit point on back of sphere
final t1 = tca + thc;
// test to see if both t0 and t1 are behind the ray - if so, return null
if (t0 < 0 && t1 < 0) return null;
// test to see if t0 is behind the ray:
// if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
// in order to always return an intersect point that is in front of the ray.
if (t0 < 0) return at(t1, target);
// else t0 is in front of the ray, so return the first collision point scaled by t0
return at(t0, target);
}