예제 #1
0
        private static float2 Depenetrate(RectangleCollider rectangleA, RectangleCollider rectangleB)
        {
            float2 depenetration = float2.zero;
            float  depth         = float.PositiveInfinity;

            for (int i = 0; rectangleA.axes.Count > i; i++)
            {
                float2 axis    = math.normalizesafe(rectangleA.axes[i]);
                float  overlap = LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis));

                if (overlap == 0)
                {
                    return(float2.zero);
                }

                if (depth > overlap)
                {
                    depth         = overlap;
                    depenetration = axis;
                }
            }

            for (int i = 0; rectangleB.axes.Count > i; i++)
            {
                float2 axis    = math.normalizesafe(rectangleB.axes[i]);
                float  overlap = LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis));

                if (overlap == 0)
                {
                    return(float2.zero);
                }

                if (depth > overlap)
                {
                    depth         = overlap;
                    depenetration = axis;
                }
            }

            return(depenetration * depth * (math.dot(depenetration, rectangleB.position - rectangleA.position) >= 0 ? -1 : 1));
        }
예제 #2
0
        private static bool Intersects(RectangleCollider rectangle, PolygonCollider polygon)
        {
            for (int i = 0; rectangle.axes.Count > i; i++)
            {
                float2 axis = rectangle.axes[i];

                if (LineOverlap(Project(rectangle, axis), Project(polygon, axis)) == 0)
                {
                    return(false);
                }
            }

            for (int i = 0; polygon.points.Count > i; i++)
            {
                float2 axis = polygon.edges[i].perpendicular();

                if (LineOverlap(Project(rectangle, axis), Project(polygon, axis)) == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #3
0
        private static bool Intersects(RectangleCollider rectangleA, RectangleCollider rectangleB)
        {
            for (int i = 0; rectangleA.axes.Count > i; i++)
            {
                float2 axis = rectangleA.axes[i];

                if (LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis)) == 0)
                {
                    return(false);
                }
            }

            for (int i = 0; rectangleB.axes.Count > i; i++)
            {
                float2 axis = rectangleB.axes[i];

                if (LineOverlap(Project(rectangleA, axis), Project(rectangleB, axis)) == 0)
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #4
0
 private static bool ContainedBy(float2 point, RectangleCollider rectangleCollider) => rectangleCollider.radians != 0
     ? ContainedBy(point, rectangleCollider as ShapeCollider)
     : rectangleCollider.xMax > point.x && point.x > rectangleCollider.xMin && rectangleCollider.yMax > point.y && point.y > rectangleCollider.yMin;
예제 #5
0
 private static bool Intersects(PolygonCollider polygon, RectangleCollider rectangle) => Intersects(rectangle, polygon);