示例#1
0
        /// <summary>
        /// Determines whether a <see cref="MyOrientedBoundingBox"/> contains a <see cref="MyBoundingSphere"/>.
        /// </summary>
        /// <param name="sphere">The sphere to test.</param>
        /// <param name="IgnoreScale">Optimize the check operation by assuming that <see cref="MyOrientedBoundingBox"/> has no scaling applied</param>
        /// <returns>The type of containment the two objects have.</returns>
        /// <remarks>
        /// This method is not designed for <see cref="MyOrientedBoundingBox"/> 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 MyContainmentType Contains(MyBoundingSphere sphere, bool IgnoreScale = false)
        {
            MyMatrix invTrans;
            MyMatrix.Invert(ref Transformation, out invTrans);

            // Transform sphere center into the obb coordinates
            MyVector3 locCenter;
            MyVector3.TransformCoordinate(ref sphere.Center, ref invTrans, out locCenter);

            float locRadius;
            if (IgnoreScale)
                locRadius = sphere.Radius;
            else
            {
                // Transform sphere radius into the obb coordinates
                MyVector3 vRadius = MyVector3.UnitX * sphere.Radius;
                MyVector3.TransformNormal(ref vRadius, ref invTrans, out vRadius);
                locRadius = vRadius.Length();
            }

            //Perform regular BoundingBox to BoundingSphere containment check
            MyVector3 minusExtens = -Extents;
            MyVector3 vector;
            MyVector3.Clamp(ref locCenter, ref minusExtens, ref Extents, out vector);
            float distance = MyVector3.DistanceSquared(locCenter, vector);

            if (distance > locRadius * locRadius)
                return MyContainmentType.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.Z - minusExtens.Z > locRadius))))
            {
                return MyContainmentType.Contains;
            }

            return MyContainmentType.Intersects;
        }