예제 #1
0
        public static bool SatIntersectionTest(this IIntersectable A, IIntersectable B)
        {
            if (A.GetSatProjectionAxes().Length == 0 || B.GetSatProjectionAxes().Length == 0)
            {
                return(false);
            }

            List <Vector2> axes = new List <Vector2>();

            axes.AddRange(A.GetSatProjectionAxes());
            axes.AddRange(B.GetSatProjectionAxes());

            foreach (Vector2 axis in axes)
            {
                Range projectionA = A.Get1dProjectionOntoAxis(axis);
                Range projectionB = B.Get1dProjectionOntoAxis(axis);

                if (!IsThere1dProjectionOverlap(projectionA, projectionB))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        public static Vector2?SatIntersectionTestGetMtv(this IIntersectable A, IIntersectable B)
        {
            if (A.GetSatProjectionAxes().Length == 0 || B.GetSatProjectionAxes().Length == 0)
            {
                return(null);
            }

            List <Vector2> axes = new List <Vector2>();

            axes.AddRange(A.GetSatProjectionAxes());
            axes.AddRange(B.GetSatProjectionAxes());
            axes = new HashSet <Vector2>(axes).ToList(); // remove duplicated axes

            float   minOverlap     = float.PositiveInfinity;
            Vector2?minOverlapAxis = null;

            foreach (Vector2 axis in axes)
            {
                Range projectionA = A.Get1dProjectionOntoAxis(axis);
                Range projectionB = B.Get1dProjectionOntoAxis(axis);

                if (!IsThere1dProjectionOverlap(projectionA, projectionB))
                {
                    return(null);
                }

                float overlap = Get1dProjectionOverlap(projectionA, projectionB);

                if (Math.Abs(overlap) < Math.Abs(minOverlap))
                {
                    minOverlap     = overlap;
                    minOverlapAxis = axis;
                }
            }

            if (minOverlapAxis == null)
            {
                return(null);
            }

            return(minOverlapAxis * minOverlap);
        }