Exemplo n.º 1
0
 public bool Overlap(Projection p)
 {
     return !(this.Min > p.Max || p.Min > this.Max);
 }
Exemplo n.º 2
0
 public bool Contains(Projection p)
 {
     return (this.Min <= p.Min && this.Max >= p.Max);
 }
Exemplo n.º 3
0
 public double GetOverlap(Projection p)
 {
     return !this.Overlap(p) ?
         0.0 :
         Math.Abs(Math.Max(this.Min, p.Min) - Math.Min(this.Max, p.Max));
 }
Exemplo n.º 4
0
 public bool Contains(Projection p)
 {
     return(this.Min <= p.Min && this.Max >= p.Max);
 }
Exemplo n.º 5
0
 public double GetOverlap(Projection p)
 {
     return(!this.Overlap(p) ?
            0.0 :
            Math.Abs(Math.Max(this.Min, p.Min) - Math.Min(this.Max, p.Max)));
 }
Exemplo n.º 6
0
 public bool Overlap(Projection p)
 {
     return(!(this.Min > p.Max || p.Min > this.Max));
 }
Exemplo n.º 7
0
        public bool MinimumTranslationVectorWithContainment(
            Vector2[] vertices1,
            Vector2[] vertices2,
            out MinimumTranslationVector?mtv)
        {
            double  overlap  = Double.PositiveInfinity; // really large value;
            Vector2 smallest = default(Vector2);

            Vector2[] axes1 = GetAxes(vertices1);
            Vector2[] axes2 = GetAxes(vertices2);
            // loop over the axes1
            for (int i = 0; i < axes1.Length; i++)
            {
                Vector2 axis = axes1[i];
                // project both shapes onto the axis
                Projection p1 = Project(vertices1, axis);
                Projection p2 = Project(vertices2, axis);
                // do the projections overlap?
                if (!p1.Overlap(p2))
                {
                    // then we can guarantee that the shapes do not overlap
                    mtv = null;
                    return(false);
                }
                else
                {
                    // get the overlap
                    double o = p1.GetOverlap(p2);
                    // check for containment
                    if (p1.Contains(p2) || p2.Contains(p1))
                    {
                        // get the overlap plus the distance from the minimum end points
                        double mins = Math.Abs(p1.Min - p2.Min);
                        double maxs = Math.Abs(p1.Max - p2.Max);
                        // NOTE: depending on which is smaller you may need to
                        // negate the separating axis!!
                        if (mins < maxs)
                        {
                            o += mins;
                        }
                        else
                        {
                            o += maxs;
                        }
                    }
                    // check for minimum
                    if (o < overlap)
                    {
                        // then set this one as the smallest
                        overlap  = o;
                        smallest = axis;
                    }
                }
            }
            // loop over the axes2
            for (int i = 0; i < axes2.Length; i++)
            {
                Vector2 axis = axes2[i];
                // project both shapes onto the axis
                Projection p1 = Project(vertices1, axis);
                Projection p2 = Project(vertices2, axis);
                // do the projections overlap?
                if (!p1.Overlap(p2))
                {
                    // then we can guarantee that the shapes do not overlap
                    mtv = null;
                    return(false);
                }
                else
                {
                    // get the overlap
                    double o = p1.GetOverlap(p2);
                    // check for containment
                    if (p1.Contains(p2) || p2.Contains(p1))
                    {
                        // get the overlap plus the distance from the minimum end points
                        double mins = Math.Abs(p1.Min - p2.Min);
                        double maxs = Math.Abs(p1.Max - p2.Max);
                        // NOTE: depending on which is smaller you may need to
                        // negate the separating axis!!
                        if (mins < maxs)
                        {
                            o += mins;
                        }
                        else
                        {
                            o += maxs;
                        }
                    }
                    // check for minimum
                    if (o < overlap)
                    {
                        // then set this one as the smallest
                        overlap  = o;
                        smallest = axis;
                    }
                }
            }
            mtv = new MinimumTranslationVector(smallest, overlap);
            // if we get here then we know that every axis had overlap on it
            // so we can guarantee an intersection
            return(true);
        }
Exemplo n.º 8
0
        public bool MinimumTranslationVector(
            Vector2[] vertices1,
            Vector2[] vertices2,
            out MinimumTranslationVector?mtv)
        {
            double  overlap  = Double.PositiveInfinity; // really large value;
            Vector2 smallest = default(Vector2);

            Vector2[] axes1 = GetAxes(vertices1);
            Vector2[] axes2 = GetAxes(vertices2);
            // loop over the axes1
            for (int i = 0; i < axes1.Length; i++)
            {
                Vector2 axis = axes1[i];
                // project both shapes onto the axis
                Projection p1 = Project(vertices1, axis);
                Projection p2 = Project(vertices2, axis);
                // do the projections overlap?
                if (!p1.Overlap(p2))
                {
                    // then we can guarantee that the shapes do not overlap
                    mtv = null;
                    return(false);
                }
                else
                {
                    // get the overlap
                    double o = p1.GetOverlap(p2);
                    // check for minimum
                    if (o < overlap)
                    {
                        // then set this one as the smallest
                        overlap  = o;
                        smallest = axis;
                    }
                }
            }
            // loop over the axes2
            for (int i = 0; i < axes2.Length; i++)
            {
                Vector2 axis = axes2[i];
                // project both shapes onto the axis
                Projection p1 = Project(vertices1, axis);
                Projection p2 = Project(vertices2, axis);
                // do the projections overlap?
                if (!p1.Overlap(p2))
                {
                    // then we can guarantee that the shapes do not overlap
                    mtv = null;
                    return(false);
                }
                else
                {
                    // get the overlap
                    double o = p1.GetOverlap(p2);
                    // check for minimum
                    if (o < overlap)
                    {
                        // then set this one as the smallest
                        overlap  = o;
                        smallest = axis;
                    }
                }
            }
            mtv = new MinimumTranslationVector(smallest, overlap);
            // if we get here then we know that every axis had overlap on it
            // so we can guarantee an intersection
            return(true);
        }