Пример #1
0
 public Vector2? Intersects(CollisionPolygon other)
 {
     return TestForIntersection(this, other);
 }
Пример #2
0
 public static Projection ProjectPolygonOntoAxis(CollisionPolygon polygon, Vector2 axis)
 {
     float min = float.PositiveInfinity;
     float max = float.NegativeInfinity;
     foreach (Vector2 vertex in polygon.VertexList)
     {
         float value = Vector2.Dot(vertex, axis);
         min = Math.Min(min, value);
         max = Math.Max(max, value);
     }
     return new Projection(min, max);
 }
Пример #3
0
        // uses the separating axis theorem to look for a collision between these two polygons
        // this will only work if the polygons are convex. Do not use concave polygons.
        // Instead, decompose concave polygons into a composition of convex polygons.
        //
        // if there is no collision, null is returned
        // otherwise, the minimum transport vector to get out of the collision is returned
        // the minimum transport vector is intended to move polygon1 away from polygon2
        public static Vector2? TestForIntersection(CollisionPolygon polygon1, CollisionPolygon polygon2)
        {
            float transportMagnitude = float.PositiveInfinity;
            Vector2? transportAxis = null;

            foreach (Vector2 axis in polygon1.AxesList)
            {
                Projection projection1 = ProjectPolygonOntoAxis(polygon1, axis);
                Projection projection2 = ProjectPolygonOntoAxis(polygon2, axis);

                //If there is no overlap, we can conclude the polygons do not intersect
                if (!projection1.OverlapsWith(projection2))
                    return null;

                float o = projection1.CalculateOverlap(projection2);
                if (o < transportMagnitude)
                {
                    transportMagnitude = o;
                    transportAxis = axis;
                }
            }

            foreach (Vector2 axis in polygon2.AxesList)
            {
                Projection projection1 = ProjectPolygonOntoAxis(polygon1, axis);
                Projection projection2 = ProjectPolygonOntoAxis(polygon2, axis);

                //If there is no overlap, we can conclude the polygons do not intersect
                if (!projection1.OverlapsWith(projection2))
                    return null;

                float o = projection1.CalculateOverlap(projection2);
                if (o < transportMagnitude)
                {
                    transportMagnitude = o;
                    transportAxis = axis;
                }
            }

            return transportMagnitude * transportAxis;
        }