public static Vector2 RayBSDistance(GSphere _sphere, GRay _ray) { RayBSCalculate(_sphere, _ray, out float dotOffsetDirection, out float discriminant); if (discriminant < 0) { return(Vector2.one * -1); } discriminant = Mathf.Sqrt(discriminant); float t0 = -dotOffsetDirection - discriminant; float t1 = -dotOffsetDirection + discriminant; if (t0 < 0) { t0 = t1; } return(new Vector2(t0, t1)); }
static void RayBSCalculate(GSphere _sphere, GRay _ray, out float dotOffsetDirection, out float discriminant) { Vector3 offset = _ray.origin - _sphere.center; dotOffsetDirection = Vector3.Dot(_ray.direction, offset); float sqrRadius = _sphere.radius * _sphere.radius; float radiusDelta = Vector3.Dot(offset, offset) - sqrRadius; discriminant = -1; if (dotOffsetDirection > 0 && radiusDelta > 0) { return; } float dotOffset = Vector3.Dot(offset, offset); discriminant = dotOffsetDirection * dotOffsetDirection - dotOffset + sqrRadius; }
public static bool RayBSIntersect(GSphere _sphere, GRay _ray) { RayBSCalculate(_sphere, _ray, out float dotOffsetDirection, out float discriminant); return(discriminant >= 0); }