Beispiel #1
0
    def convexPoly(
            self,
            vertices,
            dynamic=True,
            density=1.0,
            restitution=0.16,
            friction=0.5):
        """ Add a complex polygon with vertices in absolute positions (meters or pixels, according
            to INPUT_PIXELS or INPUT_METERS). This function does the reduction and convec hulling
            of the poly, and calls add_poly(...)

            Parameters:
              vertices .. absolute vertices positions
              other ..... see [physics parameters]

            Return: box2d.b2Body
        """
        # NOTE: Box2D has a maximum poly vertex count, defined in Common/box2d.b2Settings.h (box2d.b2_maxPolygonVertices)
        # We need to make sure, that we reach that by reducing the poly with increased tolerance
        # Reduce Polygon
        tolerance = 10  # 5
        v_new = vertices
        while len(v_new) > box2d.b2_maxPolygonVertices:
            tolerance += 1
            v_new = tools_poly.reduce_poly(vertices, tolerance)

        print "convexPoly: Polygon reduced from %i to %i vertices | tolerance: %i" % (len(vertices), len(v_new), tolerance)
        vertices = v_new

        # So poly should be alright now
        # Continue reducing the vertecs
        vertices_orig_reduced = vertices
        vertices = tools_poly.poly_center_vertices(vertices)

        vertices = tools_poly.convex_hull(vertices)

        if len(vertices) < 3:
            return

        # Define the body
        x, y = tools_poly.calc_center(vertices_orig_reduced)
        return self.poly((x, y), vertices, dynamic,
                         density, restitution, friction)
Beispiel #2
0
    def convexPoly(self,
                   vertices,
                   dynamic=True,
                   density=1.0,
                   restitution=0.16,
                   friction=0.5):
        """ Add a complex polygon with vertices in absolute positions (meters or pixels, according
            to INPUT_PIXELS or INPUT_METERS). This function does the reduction and convec hulling
            of the poly, and calls add_poly(...)

            Parameters:
              vertices .. absolute vertices positions
              other ..... see [physics parameters]

            Return: box2d.b2Body
        """
        # NOTE: Box2D has a maximum poly vertex count, defined in Common/box2d.b2Settings.h (box2d.b2_maxPolygonVertices)
        # We need to make sure, that we reach that by reducing the poly with increased tolerance
        # Reduce Polygon
        tolerance = 10  # 5
        v_new = vertices
        while len(v_new) > box2d.b2_maxPolygonVertices:
            tolerance += 1
            v_new = tools_poly.reduce_poly(vertices, tolerance)

        print "convexPoly: Polygon reduced from %i to %i vertices | tolerance: %i" % (
            len(vertices), len(v_new), tolerance)
        vertices = v_new

        # So poly should be alright now
        # Continue reducing the vertecs
        vertices_orig_reduced = vertices
        vertices = tools_poly.poly_center_vertices(vertices)

        vertices = tools_poly.convex_hull(vertices)

        if len(vertices) < 3:
            return

        # Define the body
        x, y = tools_poly.calc_center(vertices_orig_reduced)
        return self.poly((x, y), vertices, dynamic, density, restitution,
                         friction)