public static bool BoxIntersectsSphere(ref BoundingBox box, ref BoundingSphere sphere) { Vector3 result; Vector3.Clamp(ref sphere.Center, ref box.Minimum, ref box.Maximum, out result); return((double)Vector3.DistanceSquared(sphere.Center, result) <= (double)sphere.Radius * (double)sphere.Radius); }
public static ContainmentType BoxContainsSphere(ref BoundingBox box, ref BoundingSphere sphere) { Vector3 result; Vector3.Clamp(ref sphere.Center, ref box.Minimum, ref box.Maximum, out result); if ((double)Vector3.DistanceSquared(sphere.Center, result) > (double)sphere.Radius * (double)sphere.Radius) { return(ContainmentType.Disjoint); } return((double)box.Minimum.X + (double)sphere.Radius <= (double)sphere.Center.X && (double)sphere.Center.X <= (double)box.Maximum.X - (double)sphere.Radius && ((double)box.Maximum.X - (double)box.Minimum.X > (double)sphere.Radius && (double)box.Minimum.Y + (double)sphere.Radius <= (double)sphere.Center.Y) && ((double)sphere.Center.Y <= (double)box.Maximum.Y - (double)sphere.Radius && (double)box.Maximum.Y - (double)box.Minimum.Y > (double)sphere.Radius && ((double)box.Minimum.Z + (double)sphere.Radius <= (double)sphere.Center.Z && (double)sphere.Center.Z <= (double)box.Maximum.Z - (double)sphere.Radius)) && (double)box.Maximum.X - (double)box.Minimum.X > (double)sphere.Radius ? ContainmentType.Contains : ContainmentType.Intersects); }
/// <summary> /// Determines whether a <see cref="SharpDX.OrientedBoundingBox"/> contains a <see cref="SharpDX.BoundingSphere"/>. /// </summary> /// <param name="sphere">The sphere to test.</param> /// <param name="IgnoreScale">Optimize the check operation by assuming that <see cref="SharpDX.OrientedBoundingBox"/> has no scaling applied</param> /// <returns>The type of containment the two objects have.</returns> /// <remarks> /// This method is not designed for <see cref="SharpDX.OrientedBoundingBox"/> which has a non-uniform scaling applied to its transformation matrix. /// But any type of scaling applied using Scale method will keep this method accurate. /// </remarks> public ContainmentType Contains(BoundingSphere sphere, bool IgnoreScale = false) { Matrix invTrans; Matrix.Invert(ref Transformation, out invTrans); // Transform sphere center into the obb coordinates Vector3 locCenter; Vector3.TransformCoordinate(ref sphere.Center, ref invTrans, out locCenter); float locRadius; if (IgnoreScale) { locRadius = sphere.Radius; } else { // Transform sphere radius into the obb coordinates Vector3 vRadius = Vector3.UnitX * sphere.Radius; Vector3.TransformNormal(ref vRadius, ref invTrans, out vRadius); locRadius = vRadius.Length(); } //Perform regular BoundingBox to BoundingSphere containment check Vector3 minusExtens = -Extents; Vector3 vector; Vector3.Clamp(ref locCenter, ref minusExtens, ref Extents, out vector); float distance = Vector3.DistanceSquared(locCenter, vector); if (distance > locRadius * locRadius) { return(ContainmentType.Disjoint); } if ((((minusExtens.X + locRadius <= locCenter.X) && (locCenter.X <= Extents.X - locRadius)) && ((Extents.X - minusExtens.X > locRadius) && (minusExtens.Y + locRadius <= locCenter.Y))) && (((locCenter.Y <= Extents.Y - locRadius) && (Extents.Y - minusExtens.Y > locRadius)) && (((minusExtens.Z + locRadius <= locCenter.Z) && (locCenter.Z <= Extents.Z - locRadius)) && (Extents.X - minusExtens.X > locRadius)))) { return(ContainmentType.Contains); } return(ContainmentType.Intersects); }