Ejemplo n.º 1
0
    def setup(self):
        self.worldNP = render.attach_new_node('World')

        # World
        self.debugNP = self.worldNP.attach_new_node(BulletDebugNode('Debug'))
        self.debugNP.show()

        self.world = BulletWorld()
        self.world.set_gravity(LVector3(0, 0, -9.81))
        self.world.set_debug_node(self.debugNP.node())

        # Ground
        p0 = LPoint3(-20, -20, 0)
        p1 = LPoint3(-20, 20, 0)
        p2 = LPoint3(20, -20, 0)
        p3 = LPoint3(20, 20, 0)
        mesh = BulletTriangleMesh()
        mesh.add_triangle(p0, p1, p2)
        mesh.add_triangle(p1, p2, p3)
        shape = BulletTriangleMeshShape(mesh, dynamic=False)

        np = self.worldNP.attach_new_node(BulletRigidBodyNode('Mesh'))
        np.node().add_shape(shape)
        np.set_pos(0, 0, -4)
        np.set_collide_mask(BitMask32.all_on())

        self.world.attach(np.node())

        # Soft body world information
        info = self.world.get_world_info()
        info.set_air_density(1.2)
        info.set_water_density(0)
        info.set_water_offset(0)
        info.set_water_normal(LVector3(0, 0, 0))

        ## Softbody - From points/indices
        #import cube
        #points = [LPoint3(x,y,z) * 3 for x,y,z in cube.nodes]
        #indices = sum([list(x) for x in cube.elements], [])

        #node = BulletSoftBodyNode.make_tet_mesh(info, points, indices, True)
        #node.set_volume_mass(300);
        #node.get_shape(0).set_margin(0.01)
        #node.get_material(0).set_linear_stiffness(0.8)
        #node.get_cfg().set_positions_solver_iterations(1)
        #node.get_cfg().clear_all_collision_flags()
        #node.get_cfg().set_collision_flag(
        #BulletSoftBodyConfig.CF_cluster_soft_soft, True)
        #node.get_cfg().set_collision_flag(
        #BulletSoftBodyConfig.CF_cluster_rigid_soft, True)
        #node.generate_clusters(16)

        #softNP = self.worldNP.attach_new_node(node)
        #softNP.set_pos(0, 0, 8)
        #softNP.set_hpr(0, 0, 45)
        #self.world.attach(node)

        # Softbody - From tetgen data
        ele = open('models/cube/cube.1.ele', 'r').read()
        face = open('models/cube/cube.1.face', 'r').read()
        node = open('models/cube/cube.1.node', 'r').read()

        node = BulletSoftBodyNode.make_tet_mesh(info, ele, face, node)
        node.set_name('Tetra')
        node.set_volume_mass(300)
        node.get_shape(0).set_margin(0.01)
        node.get_material(0).set_linear_stiffness(0.1)
        node.get_cfg().set_positions_solver_iterations(1)
        node.get_cfg().clear_all_collision_flags()
        node.get_cfg().set_collision_flag(
            BulletSoftBodyConfig.CF_cluster_soft_soft, True)
        node.get_cfg().setCollisionFlag(
            BulletSoftBodyConfig.CF_cluster_rigid_soft, True)
        node.generate_clusters(6)

        softNP = self.worldNP.attach_new_node(node)
        softNP.set_pos(0, 0, 8)
        softNP.set_hpr(45, 0, 0)
        self.world.attach(node)

        # Option 1:
        visNP = loader.load_model('models/cube/cube.egg')
        visNP.reparent_to(softNP)
        geom = (visNP.findAllMatches('**/+GeomNode').getPath(
            0).node().modifyGeom(0))
        node.link_geom(geom)