예제 #1
0
        public bool MinimumTranslationVectorWithContainment(Vector2[] vertices1, Vector2[] vertices2, out MinimumTranslationVector?mtv)
        {
            double  overlap  = Double.PositiveInfinity;
            Vector2 smallest = default(Vector2);

            Vector2[] axes1 = GetAxes(vertices1);
            Vector2[] axes2 = GetAxes(vertices2);

            bool MTVWC(Vector2[] axes)
            {
                for (int i = 0; i < axes.Length; i++)
                {
                    Vector2    axis = axes[i];
                    Projection p1   = Project(vertices1, axis);
                    Projection p2   = Project(vertices2, axis);
                    if (!p1.Overlap(p2))
                    {
                        return(false);
                    }
                    else
                    {
                        double o = p1.GetOverlap(p2);
                        if (p1.Contains(p2) || p2.Contains(p1))
                        {
                            double mins = Math.Abs(p1.Min - p2.Min);
                            double maxs = Math.Abs(p1.Max - p2.Max);
                            o += mins < maxs ? mins : maxs;
                        }

                        if (o < overlap)
                        {
                            overlap  = o;
                            smallest = axis;
                        }
                    }
                }
                return(true);
            }

            if (MTVWC(axes1) == false)
            {
                mtv = default(MinimumTranslationVector?);
                return(false);
            }

            if (MTVWC(axes2) == false)
            {
                mtv = default(MinimumTranslationVector?);
                return(false);
            }

            mtv = new MinimumTranslationVector(smallest, overlap);
            return(true);
        }
예제 #2
0
        public bool MinimumTranslationVector(Vector2[] vertices1, Vector2[] vertices2, out MinimumTranslationVector?mtv)
        {
            double  overlap  = Double.PositiveInfinity;
            Vector2 smallest = default(Vector2);

            Vector2[] axes1 = GetAxes(vertices1);
            Vector2[] axes2 = GetAxes(vertices2);

            for (int i = 0; i < axes1.Length; i++)
            {
                Vector2    axis = axes1[i];
                Projection p1   = Project(vertices1, axis);
                Projection p2   = Project(vertices2, axis);
                if (!p1.Overlap(p2))
                {
                    mtv = default(MinimumTranslationVector?);
                    return(false);
                }
                else
                {
                    double o = p1.GetOverlap(p2);
                    if (o < overlap)
                    {
                        overlap  = o;
                        smallest = axis;
                    }
                }
            }

            for (int i = 0; i < axes2.Length; i++)
            {
                Vector2    axis = axes2[i];
                Projection p1   = Project(vertices1, axis);
                Projection p2   = Project(vertices2, axis);
                if (!p1.Overlap(p2))
                {
                    mtv = default(MinimumTranslationVector?);
                    return(false);
                }
                else
                {
                    double o = p1.GetOverlap(p2);
                    if (o < overlap)
                    {
                        overlap  = o;
                        smallest = axis;
                    }
                }
            }
            mtv = new MinimumTranslationVector(smallest, overlap);
            return(true);
        }