Beispiel #1
0
        /// <summary>
        /// Returns a polygon, based on this polygon, that is reflected along the given axes.
        /// </summary>
        /// <param name="reflectX">If true, the polygon will be reflected along the x-axis. </param>
        /// <param name="reflectY">If true, the polygon will be reflected along the y-axis. </param>
        /// <returns></returns>
        public Polygon2D GetReflected(bool reflectX, bool reflectY)
        {
            Polygon2D polygonResult = new Polygon2D();

            foreach (Vector2D point in this.Points)
            {
                double resultX = point.X;
                double resultY = point.Y;

                if (reflectX)
                {
                    resultX = -resultX;
                }
                if (reflectY)
                {
                    resultY = -resultY;
                }

                polygonResult.Points.Add(new Vector2D(resultX, resultY));
            }

            return(polygonResult);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the result of a collision check of this polygon with the given polygon.
        /// </summary>
        /// <param name="polygon">A polygon to check against. </param>
        /// <returns></returns>
        public PolygonCollisionResult PolygonCollision(Polygon2D polygon)
        {
            PolygonCollisionResult result = new PolygonCollisionResult();

            if (polygon == null) // No collsion checking possible.
            {
                return(result);
            }
            else // Initialization.
            {
                result.Intersect = true;
            }

            int edgeCountA = this.Points.Count;
            int edgeCountB = polygon.Points.Count;

            //float minIntervalDistance = float.PositiveInfinity;

            Vector2D vectAxisTranslation = new Vector2D();
            Vector2D vectEdge;

            // Loop through all the edges of both polygons
            for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++)
            {
                if (edgeIndex < edgeCountA) // Iterating over the edges of polygon A.
                {
                    if (edgeIndex == edgeCountA - 1)
                    {
                        vectEdge = this.Points[0] - this.Points[edgeIndex];
                    }
                    else
                    {
                        vectEdge = this.Points[edgeIndex + 1] - this.Points[edgeIndex];
                    }
                }
                else // Iterating over the edges of polygon B.
                {
                    if (edgeIndex == edgeCountA + edgeCountB - 1)
                    {
                        vectEdge = polygon.Points[0] - polygon.Points[edgeIndex - edgeCountA];
                    }
                    else
                    {
                        vectEdge = polygon.Points[edgeIndex - edgeCountA + 1] - polygon.Points[edgeIndex - edgeCountA];
                    }
                }

                // ===== 1. Find if the polygons are currently intersecting =====

                // Find the axis perpendicular to the current edge
                Vector2D vectAxis = new Vector2D(-vectEdge.Y, vectEdge.X);
                vectAxis = vectAxis.GetNormalized();

                // Find the projection of the polygon on the current axis
                Interval intervalA = this.Project(vectAxis);
                Interval intervalB = polygon.Project(vectAxis);

                // Check if the polygon projections are currentlty intersecting
                if (intervalA.GetIntervalDistance(intervalB) > 0)
                {
                    result.Intersect = false;
                }

                //// ===== 2. Now find if the polygons *will* intersect =====

                //// Project the velocity on the current axis
                //float velocityProjection = (float)vectAxis.GetDotProduct(velocity);

                //// Get the projection of polygon A during the movement
                //if (velocityProjection < 0)
                //{
                //    intervalA.min += velocityProjection;
                //}
                //else
                //{
                //    intervalA.max += velocityProjection;
                //}

                //// Do the same test as above for the new projection
                //float intervalDistance = intervalA.GetIntervalDistance(intervalB);

                //if (intervalDistance > 0)
                //    result.WillIntersect = false;

                //// If the polygons are not intersecting and won't intersect, exit the loop
                //if (!result.Intersect && !result.WillIntersect)
                //    break;

                //// Check if the current interval distance is the minimum one. If so store
                //// the interval distance and the current distance.
                //// This will be used to calculate the minimum translation vector
                //intervalDistance = Math.Abs(intervalDistance);

                //if (intervalDistance < minIntervalDistance)
                //{
                //    minIntervalDistance = intervalDistance;
                //    vectAxisTranslation = vectAxis;

                //    csVector2D d = this.vectCenter - polygon.vectCenter;

                //    if (d.GetDotProduct(vectAxisTranslation) < 0)
                //        vectAxisTranslation = -vectAxisTranslation;
                //}
            }

            //// The minimum translation vector
            //// can be used to push the polygons appart.
            //result.MinimumTranslationVector = vectAxisTranslation * minIntervalDistance;

            return(result);
        }
Beispiel #3
0
 /// <summary>
 /// Returns the result of a collision check of the given polygons.
 /// </summary>
 /// <param name="polygonA">A polygon to check. </param>
 /// <param name="polygonB">A polygon to check against. </param>
 /// <returns></returns>
 public static PolygonCollisionResult PolygonCollision(Polygon2D polygonA, Polygon2D polygonB)
 {
     return(polygonA.PolygonCollision(polygonB));
 }