containsPoint() public method

essentially what the algorithm is doing is shooting a ray from point out. If it intersects an odd number of polygon sides we know it is inside the polygon.
public containsPoint ( Vector2 point ) : bool
point Vector2 Point.
return bool
コード例 #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(
                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);
        }
コード例 #2
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);
        }
コード例 #3
0
ファイル: ShapeCollisionsPoint.cs プロジェクト: prime31/Nez
		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;
		}
コード例 #4
0
ファイル: ShapeCollisionsCircle.cs プロジェクト: prime31/Nez
		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;
		}