Projection for Separating Axis Theorem implementation.
Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="vertices1"></param>
        /// <param name="vertices2"></param>
        /// <param name="mtv"></param>
        /// <returns></returns>
        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 = default(MinimumTranslationVector?);
                    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 = Abs(p1.Min - p2.Min);
                        double maxs = 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 = default(MinimumTranslationVector?);
                    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 = Abs(p1.Min - p2.Min);
                        double maxs = 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);
        }
Ejemplo n.º 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="p"></param>
 /// <returns></returns>
 public bool Contains(Projection p) => (this.Min <= p.Min && this.Max >= p.Max);
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="vertices1"></param>
        /// <param name="vertices2"></param>
        /// <param name="mtv"></param>
        /// <returns></returns>
        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 = default(MinimumTranslationVector?);
                    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 = default(MinimumTranslationVector?);
                    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);
        }
Ejemplo n.º 4
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="p"></param>
 /// <returns></returns>
 public double GetOverlap(Projection p) => !this.Overlap(p) ? 0.0 : Abs(Max(this.Min, p.Min) - Min(this.Max, p.Max));
Ejemplo n.º 5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="p"></param>
 /// <returns></returns>
 public bool Overlap(Projection p) => !(this.Min > p.Max || p.Min > this.Max);
Ejemplo n.º 6
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="p"></param>
 /// <returns></returns>
 public bool Contains(Projection p) => (this.Min <= p.Min && this.Max >= p.Max);
Ejemplo n.º 7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="p"></param>
 /// <returns></returns>
 public double GetOverlap(Projection p) => !this.Overlap(p) ? 0.0 : Abs(Max(this.Min, p.Min) - Min(this.Max, p.Max));
Ejemplo n.º 8
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="p"></param>
 /// <returns></returns>
 public bool Overlap(Projection p) => !(this.Min > p.Max || p.Min > this.Max);