public static bool RayIntersectsSphere(Ray3f ray, Sphere3f sphere, out float t) { t = 0; Vector3f m = ray.Position - sphere.Center; float b = Vector3f.Dot(m, ray.Direction); float c = Vector3f.Dot(m, m) - sphere.Radius2; if (c > 0.0f && b > 0.0f) { return(false); } float discr = b * b - c; if (discr < 0.0f) { return(false); } t = -b - (float)Math.Sqrt(discr); if (t < 0) { t = 0; } return(true); }
public static bool BoxIntersectsCircle(Box3f box, Sphere3f sphere) { if (sphere.Center.x - sphere.Radius > box.Max.x || sphere.Center.x + sphere.Radius < box.Min.x) { return(false); } if (sphere.Center.y - sphere.Radius > box.Max.y || sphere.Center.y + sphere.Radius < box.Min.y) { return(false); } if (sphere.Center.z - sphere.Radius > box.Max.z || sphere.Center.z + sphere.Radius < box.Min.z) { return(false); } return(true); }
public static bool SphereContainsPoint(Sphere3f sphere, Vector3f p) { Vector3f diff = p - sphere.Center; return(diff.SqrMagnitude < sphere.Radius2); }