Ejemplo n.º 1
0
        static bool PolygonCollision(PolygonForCollision polygonA, PolygonForCollision polygonB)
        {
            bool Intersect = true;

            int edgeCountA = polygonA.Edges.Count;
            int edgeCountB = polygonB.Edges.Count;
            VectorForCollision edge;

            for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++)
            {
                if (edgeIndex < edgeCountA)
                {
                    edge = polygonA.Edges[edgeIndex];
                }
                else
                {
                    edge = polygonB.Edges[edgeIndex - edgeCountA];
                }

                VectorForCollision axis = new VectorForCollision(-edge.Y, edge.X);
                axis.Normalize();

                float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
                ProjectPolygon(axis, polygonA, ref minA, ref maxA);
                ProjectPolygon(axis, polygonB, ref minB, ref maxB);

                if (IntervalDistance(minA, maxA, minB, maxB) > 0)
                {
                    Intersect = false;
                }
            }

            return(Intersect);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 서로 다른 두 다각형의 충돌 체크
        /// </summary>
        /// <param name="poly1">다각형 1</param>
        /// <param name="poly2">다각형 2</param>
        /// <returns>충돌 여부</returns>
        public static bool CheckPolygon(PointF[] poly1, PointF[] poly2)
        {
            PolygonForCollision p1 = new PolygonForCollision();
            PolygonForCollision p2 = new PolygonForCollision();

            p1.Points.AddRange(poly1.Select(x => new VectorForCollision(x.X, x.Y)));
            p2.Points.AddRange(poly2.Select(x => new VectorForCollision(x.X, x.Y)));
            p1.BuildEdges();
            p2.BuildEdges();
            return(PolygonCollision(p1, p2));
        }
Ejemplo n.º 3
0
        static void ProjectPolygon(VectorForCollision axis, PolygonForCollision polygon, ref float min, ref float max)
        {
            float d = axis.DotProduct(polygon.Points[0]);

            min = d;
            max = d;
            for (int i = 0; i < polygon.Points.Count; i++)
            {
                d = polygon.Points[i].DotProduct(axis);
                if (d < min)
                {
                    min = d;
                }
                else
                {
                    if (d > max)
                    {
                        max = d;
                    }
                }
            }
        }