Exemplo n.º 1
0
    def addTetraMeshSB(self,vertices, faces,normals = None,ghost=False,**kw):
        #step 1) create GeomVertexData and add vertex information
        # Soft body world information
        info = self.world.getWorldInfo()
        info.setAirDensity(1.2)
        info.setWaterDensity(0)
        info.setWaterOffset(0)
        info.setWaterNormal(Vec3(0, 0, 0))
    
        points = [Point3(x,y,z) * 3 for x,y,z in vertices]
        indices = sum([list(x) for x in faces], [])
        #step 4) create the bullet softbody and node
        bodyNode = BulletSoftBodyNode.makeTetMesh(info, points, indices, True)

        bodyNode.setName('Tetra')
        bodyNode.setVolumeMass(150000)
        bodyNode.getShape(0).setMargin(0.01)
        bodyNode.getMaterial(0).setLinearStiffness(0.9)
        bodyNode.getCfg().setPositionsSolverIterations(4)
        bodyNode.getCfg().clearAllCollisionFlags()
        bodyNode.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterSoftSoft, True)
        bodyNode.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterRigidSoft, True)
        bodyNode.generateClusters(12)
        bodyNode.setPose(True, True)
        bodyNP = self.worldNP.attachNewNode(bodyNode)
        
        geom = BulletHelper.makeGeomFromFaces(bodyNode)
        geomNode = GeomNode('vtetra')
        geomNode.addGeom(geom)
        
#        self.setRB(bodyNP,**kw)#set po
#        inodenp.setCollideMask(BitMask32.allOn())
        self.world.attachSoftBody(bodyNode)
        geomNP = bodyNP.attachNewNode(geomNode)
        bodyNode.linkGeom(geom)
        return bodyNP,geomNP
Exemplo n.º 2
0
  def setup(self):
    self.worldNP = render.attachNewNode('World')

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

    self.world = BulletWorld()
    self.world.setGravity(Vec3(0, 0, -9.81))
    self.world.setDebugNode(self.debugNP.node())

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

    np = self.worldNP.attachNewNode(BulletRigidBodyNode('Mesh'))
    np.node().addShape(shape)
    np.setPos(0, 0, -4)
    np.setCollideMask(BitMask32.allOn())

    self.world.attachRigidBody(np.node())

    # Soft body world information
    info = self.world.getWorldInfo()
    info.setAirDensity(1.2)
    info.setWaterDensity(0)
    info.setWaterOffset(0)
    info.setWaterNormal(Vec3(0, 0, 0))

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

    #node = BulletSoftBodyNode.makeTetMesh(info, points, indices, True)
    #node.setVolumeMass(300);
    #node.getShape(0).setMargin(0.01)
    #node.getMaterial(0).setLinearStiffness(0.8)
    #node.getCfg().setPositionsSolverIterations(1)
    #node.getCfg().clearAllCollisionFlags()
    #node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterSoftSoft, True)
    #node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterRigidSoft, True)
    #node.generateClusters(16)

    #softNP = self.worldNP.attachNewNode(node)
    #softNP.setPos(0, 0, 8)
    #softNP.setHpr(0, 0, 45)
    #self.world.attachSoftBody(node)

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

    node = BulletSoftBodyNode.makeTetMesh(info, ele, face, node)
    node.setName('Tetra')
    node.setVolumeMass(300)
    node.getShape(0).setMargin(0.01)
    node.getMaterial(0).setLinearStiffness(0.1)
    node.getCfg().setPositionsSolverIterations(1)
    node.getCfg().clearAllCollisionFlags()
    node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterSoftSoft, True)
    node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterRigidSoft, True)
    node.generateClusters(6)

    softNP = self.worldNP.attachNewNode(node)
    softNP.setPos(0, 0, 8)
    softNP.setHpr(45, 0, 0)
    self.world.attachSoftBody(node)

    # Option 1:
    visNP = loader.loadModel('models/cube/cube.egg')
    visNP.reparentTo(softNP)

    geom = visNP \
        .findAllMatches('**/+GeomNode').getPath(0).node() \
        .modifyGeom(0)
    node.linkGeom(geom)
    def setup(self):
        self.worldNP = render.attachNewNode('World')

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

        self.world = BulletWorld()
        self.world.setGravity(Vec3(0, 0, -9.81))
        self.world.setDebugNode(self.debugNP.node())

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

        np = self.worldNP.attachNewNode(BulletRigidBodyNode('Mesh'))
        np.node().addShape(shape)
        np.setPos(0, 0, -4)
        np.setCollideMask(BitMask32.allOn())

        self.world.attachRigidBody(np.node())

        # Soft body world information
        info = self.world.getWorldInfo()
        info.setAirDensity(1.2)
        info.setWaterDensity(0)
        info.setWaterOffset(0)
        info.setWaterNormal(Vec3(0, 0, 0))

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

        #node = BulletSoftBodyNode.makeTetMesh(info, points, indices, True)
        #node.setVolumeMass(300);
        #node.getShape(0).setMargin(0.01)
        #node.getMaterial(0).setLinearStiffness(0.8)
        #node.getCfg().setPositionsSolverIterations(1)
        #node.getCfg().clearAllCollisionFlags()
        #node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterSoftSoft, True)
        #node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterRigidSoft, True)
        #node.generateClusters(16)

        #softNP = self.worldNP.attachNewNode(node)
        #softNP.setPos(0, 0, 8)
        #softNP.setHpr(0, 0, 45)
        #self.world.attachSoftBody(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.makeTetMesh(info, ele, face, node)
        node.setName('Tetra')
        node.setVolumeMass(300)
        node.getShape(0).setMargin(0.01)
        node.getMaterial(0).setLinearStiffness(0.1)
        node.getCfg().setPositionsSolverIterations(1)
        node.getCfg().clearAllCollisionFlags()
        node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterSoftSoft,
                                       True)
        node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFClusterRigidSoft,
                                       True)
        node.generateClusters(6)

        softNP = self.worldNP.attachNewNode(node)
        softNP.setPos(0, 0, 8)
        softNP.setHpr(45, 0, 0)
        self.world.attachSoftBody(node)

        # Option 1:
        visNP = loader.loadModel('models/cube/cube.egg')
        visNP.reparentTo(softNP)

        geom = visNP \
            .findAllMatches('**/+GeomNode').getPath(0).node() \
            .modifyGeom(0)
        node.linkGeom(geom)