Beispiel #1
0
        /// <summary>
        /// Calculates if there is a separating axis between two polygon colliders
        /// </summary>
        /// <param name="normal"></param>
        /// <param name="col1"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        static bool IsSeparatingAxis(Vector2 normal, PolygonCollider col1, PolygonCollider col2)
        {
            double min1 = double.PositiveInfinity;
            double max1 = double.NegativeInfinity;
            double min2 = double.PositiveInfinity;
            double max2 = double.NegativeInfinity;

            foreach (Vector2 point in col1.Vertices)
            {
                double projection = Vector2.Dot(point, normal);
                min1 = Math.Min(min1, projection);
                max1 = Math.Max(max1, projection);
            }

            foreach (Vector2 point in col2.Vertices)
            {
                double projection = Vector2.Dot(point, normal);
                min2 = Math.Min(min2, projection);
                max2 = Math.Max(max2, projection);
            }

            if (max1 >= min2 && max2 >= min1)
            {
                return(false);
            }

            return(true);
        }
Beispiel #2
0
        /// <summary>
        /// Checks to see if a polygon and a circle are colliding
        /// </summary>
        /// <param name="col1"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static bool IsCollidingPolygonCircle(PolygonCollider col1, CircleCollider col2)
        {
            List <Vector2> edges = new List <Vector2>();

            edges = CalculateEdges(col1.Vertices);
            for (int i = 0; i < edges.Count; i++)
            {
                if (Vector2.Distance(col2.centre, edges[i]) < col2.radius)
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// Checks to see if 2 polygons are colliding
        /// </summary>
        /// <param name="col1"></param>
        /// <param name="col2"></param>
        /// <returns></returns>
        public static bool IsCollidingPolygons(PolygonCollider col1, PolygonCollider col2)
        {
            List <Vector2> edges = CalculateEdges(col1.Vertices);

            edges.AddRange(CalculateEdges(col2.Vertices));

            List <Vector2> normals = new List <Vector2>();

            foreach (Vector2 edge in edges)
            {
                normals.Add(CalculateNormal(edge));
            }

            foreach (Vector2 normal in normals)
            {
                bool isSeparated = IsSeparatingAxis(normal, col1, col2);
                if (!isSeparated)
                {
                    return(false);
                }
            }

            return(true);
        }