getClosestPointOnPolygonToPoint() public static method

iterates all the edges of the polygon and gets the closest point on any edge to point. Returns via out the squared distance to the closest point and the normal of the edge it is on. point should be in the space of the Polygon (point - poly.position)
public static getClosestPointOnPolygonToPoint ( Vector2 points, Vector2 point, float &distanceSquared, Vector2 &edgeNormal ) : Vector2
points Vector2
point Vector2 Point.
distanceSquared float Distance squared.
edgeNormal Vector2 Edge normal.
return Vector2
Example #1
0
        public static bool circleToPolygon(Circle circle, Polygon polygon, out CollisionResult result)
        {
            result = new CollisionResult();

            // circle position in the polygons coordinates
            var poly2Circle = circle.position - polygon.position;

            // first, we need to find the closest distance from the circle to the polygon
            float distanceSquared;
            var   closestPoint = polygon.getClosestPointOnPolygonToPoint(poly2Circle, out distanceSquared, out result.normal);

            // make sure the squared distance is less than our radius squared else we are not colliding
            if (distanceSquared > circle.radius * circle.radius)
            {
                return(false);
            }

            // figure out the mtd
            var distance = Mathf.sqrt(distanceSquared);
            var mtv      = (poly2Circle - closestPoint) * ((circle.radius - distance) / distance);

            result.minimumTranslationVector = -mtv;
            result.normal.Normalize();

            return(true);
        }
        public static bool circleToPolygon(Circle circle, Polygon polygon, out CollisionResult result)
        {
            result = new CollisionResult();

            // circle position in the polygons coordinates
            var poly2Circle = circle.position - polygon.position;

            // first, we need to find the closest distance from the circle to the polygon
            float distanceSquared;
            var   closestPoint = Polygon.getClosestPointOnPolygonToPoint(
                polygon.points,
                poly2Circle,
                out distanceSquared,
                out result.normal);

            // make sure the squared distance is less than our radius squared else we are not colliding. Note that if the Circle is fully
            // contained in the Polygon the distance could be larger than the radius. Because of that we also  make sure the circle position
            // is not inside the poly.
            var circleCenterInsidePoly = polygon.containsPoint(circle.position);

            if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
            {
                return(false);
            }

            // figure out the mtv. We have to be careful to deal with circles fully contained in the polygon or with their center contained.
            Vector2 mtv;

            if (circleCenterInsidePoly)
            {
                mtv = result.normal * (Mathf.sqrt(distanceSquared) - circle.radius);
            }
            else
            {
                // if we have no distance that means the circle center is on the polygon edge. Move it only by its radius
                if (distanceSquared == 0)
                {
                    mtv = result.normal * circle.radius;
                }
                else
                {
                    var distance = Mathf.sqrt(distanceSquared);
                    mtv = -(poly2Circle - closestPoint) * ((circle.radius - distance) / distance);
                }
            }

            result.minimumTranslationVector = mtv;
            result.point = closestPoint + polygon.position;

            return(true);
        }
Example #3
0
        public static bool pointToPoly(Vector2 point, Polygon poly, out CollisionResult result)
        {
            result = new CollisionResult();

            if (poly.containsPoint(point))
            {
                float distanceSquared;
                var   closestPoint = Polygon.getClosestPointOnPolygonToPoint(poly.points, point - poly.position, out distanceSquared, out result.normal);

                result.minimumTranslationVector = result.normal * Mathf.sqrt(distanceSquared);
                result.point = closestPoint + poly.position;

                return(true);
            }

            return(false);
        }
Example #4
0
        public static bool circleToPolygon( Circle circle, Polygon polygon, out CollisionResult result )
        {
            result = new CollisionResult();

            // circle position in the polygons coordinates
            var poly2Circle = circle.position - polygon.position;

            // first, we need to find the closest distance from the circle to the polygon
            float distanceSquared;
            var closestPoint = polygon.getClosestPointOnPolygonToPoint( poly2Circle, out distanceSquared, out result.normal );

            // make sure the squared distance is less than our radius squared else we are not colliding
            if( distanceSquared > circle.radius * circle.radius )
                return false;

            // figure out the mtd
            var distance = Mathf.sqrt( distanceSquared );
            var mtv = ( poly2Circle - closestPoint ) * ( ( circle.radius - distance ) / distance );

            result.minimumTranslationVector = -mtv;
            result.normal.Normalize();

            return true;
        }