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, -2)
    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
    for i in range(50):
      p00 = Point3(-2, -2, 0)
      p10 = Point3( 2, -2, 0)
      p01 = Point3(-2,  2, 0)
      p11 = Point3( 2,  2, 0)
      node = BulletSoftBodyNode.makePatch(info, p00, p10, p01, p11, 6, 6, 0, True)
      node.generateBendingConstraints(2)
      node.getCfg().setLiftCoefficient(0.004)
      node.getCfg().setDynamicFrictionCoefficient(0.0003)
      node.getCfg().setAeroModel(BulletSoftBodyConfig.AMVertexTwoSided)
      node.setTotalMass(0.1)
      node.addForce(Vec3(0, 2, 0), 0)

      np = self.worldNP.attachNewNode(node)
      np.setPos(self.Vec3Rand() * 10 + Vec3(0, 0, 20))
      np.setHpr(self.Vec3Rand() * 16)
      self.world.attachSoftBody(node)

      fmt = GeomVertexFormat.getV3n3t2()
      geom = BulletHelper.makeGeomFromFaces(node, fmt, True)
      node.linkGeom(geom)
      nodeV = GeomNode('')
      nodeV.addGeom(geom)
      npV = np.attachNewNode(nodeV)
Example #2
0
 def egg_to_BulletTriangleMeshShape(self, modelnp):
     mesh = BulletTriangleMesh()
     for collisionNP in modelnp.findAllMatches('**/+CollisionNode'):
         collisionNode = collisionNP.node()
         for collisionPolygon in collisionNode.getSolids():
             tri_points = collisionPolygon.getPoints()
             mesh.addTriangle(tri_points[0], tri_points[1], tri_points[2])
     shape = BulletTriangleMeshShape(mesh, dynamic=False)
     return shape
Example #3
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(np.random.randn()*3, np.random.randn()*3, 0))
    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)

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

    self.ground = npp

    self.world.attachRigidBody(npp.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
    def makeSB(pos, hpr):
      model = loader.loadModel('moleculemesh/smoothed/clathrinfixsmth%02d.obj' % np.random.randint(1,21))
      geom = model.findAllMatches('**/+GeomNode').getPath(0).node().modifyGeom(0)

      geomNode = GeomNode('')
      geomNode.addGeom(geom)

      node = BulletSoftBodyNode.makeTriMesh(info, geom) 
      node.linkGeom(geomNode.modifyGeom(0))

      node.generateBendingConstraints(2)
      node.getCfg().setPositionsSolverIterations(6)
      node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFVertexFaceSoftSoft, True)
      node.randomizeConstraints()
      node.setTotalMass(50, True)
      node.getMaterial(0).setLinearStiffness(0.2)
      #node.getCfg().setDynamicFrictionCoefficient(1)
      #node.getCfg().setDampingCoefficient(0.001)
      node.getCfg().setPressureCoefficient(1500)


      softNP = self.worldNP.attachNewNode(node)
      softNP.setPos(pos)
      softNP.setHpr(hpr)
      self.world.attachSoftBody(node)

      geomNP = softNP.attachNewNode(geomNode)

      #softNP.node().appendAnchor(1, self.ground.node())
      softNP.node().appendAnchor(1217, self.ground.node())
      softNP.node().appendAnchor(1157, self.ground.node())
      softNP.node().appendAnchor(2052, self.ground.node())
      #softNP.node().appendAnchor(1800, self.ground.node())
      softNP.reparentTo(self.mybase)

    #makeSB(Point3(-3, 0, 4), (0, 0, 0))
    #makeSB(Point3(0, 0, 4), (0, 90, 90))
    makeSB(Point3(4+np.random.randn(), 2+np.random.randn(), 4), (0, 0, 0))
Example #4
0
    def setup(self):
        self.worldNP = render.attachNewNode('World')

        # World
        self.debugNP = self.worldNP.attachNewNode(BulletDebugNode('Debug'))
        self.debugNP.show()
        self.debugNP.node().showWireframe(True)
        self.debugNP.node().showConstraints(True)
        self.debugNP.node().showBoundingBoxes(False)
        self.debugNP.node().showNormals(True)

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

        # Plane (static)
        shape = BulletPlaneShape(Vec3(0, 0, 1), 0)

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

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

        # Box (dynamic)
        shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))

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

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

        self.boxNP = np  # For applying force & torque

        #np.node().notifyCollisions(True)
        #self.accept('bullet-contact-added', self.doAdded)
        #self.accept('bullet-contact-destroyed', self.doRemoved)

        # Sphere (dynamic)
        shape = BulletSphereShape(0.6)

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

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

        # Cone (dynamic)
        shape = BulletConeShape(0.6, 1.2, ZUp)

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

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

        # Capsule (dynamic)
        shape = BulletCapsuleShape(0.5, 1.0, ZUp)

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

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

        # Cyliner (dynamic)
        shape = BulletCylinderShape(0.7, 1.5, ZUp)

        np = self.worldNP.attachNewNode(BulletRigidBodyNode('Cylinder'))
        np.node().setMass(1.0)
        np.node().addShape(shape)
        np.setPos(4, 4, 4)
        np.setCollideMask(BitMask32.allOn())

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

        # Convex (dynamic)
        shape = BulletConvexHullShape()
        shape.addPoint(Point3(1, 1, 2))
        shape.addPoint(Point3(0, 0, 0))
        shape.addPoint(Point3(2, 0, 0))
        shape.addPoint(Point3(0, 2, 0))
        shape.addPoint(Point3(2, 2, 0))

        # Another way to create the convex hull shape:
        #shape = BulletConvexHullShape()
        #shape.addArray([
        #  Point3(1, 1, 2),
        #  Point3(0, 0, 0),
        #  Point3(2, 0, 0),
        #  Point3(0, 2, 0),
        #  Point3(2, 2, 0),
        #])

        # Yet another way to create the convex hull shape:
        #geom = loader.loadModel('models/box.egg')\
        #         .findAllMatches('**/+GeomNode')\
        #         .getPath(0)\
        #         .node()\
        #         .getGeom(0)
        #shape = BulletConvexHullShape()
        #shape.addGeom(geom)

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

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

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

        # Another way to create the triangle mesh shape:
        #geom = loader.loadModel('models/box.egg')\
        #         .findAllMatches('**/+GeomNode')\
        #         .getPath(0)\
        #         .node()\
        #         .getGeom(0)
        #mesh = BulletTriangleMesh()
        #mesh.addGeom(geom)
        #shape = BulletTriangleMeshShape(mesh, dynamic=False)

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

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

        # MutiSphere
        points = [Point3(-1, 0, 0), Point3(0, 0, 0), Point3(1, 0, 0)]
        radii = [.4, .8, .6]
        shape = BulletMultiSphereShape(points, radii)

        np = self.worldNP.attachNewNode(BulletRigidBodyNode('MultiSphere'))
        np.node().setMass(1.0)
        np.node().addShape(shape)
        np.setPos(8, 0, 4)
        np.setCollideMask(BitMask32.allOn())

        self.world.attachRigidBody(np.node())
  def setup(self):
    self.worldNP = render.attachNewNode('World')

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

    #self.debugNP.showTightBounds()
    #self.debugNP.showBounds()

    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, -2)
    np.setCollideMask(BitMask32.allOn())

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

    # Stair
    origin = Point3(0, 0, 0)
    size = Vec3(2, 10, 1)
    shape = BulletBoxShape(size * 0.5)
    for i in range(10):
      pos = origin + size * i
      pos.setY(0)

      np = self.worldNP.attachNewNode(BulletRigidBodyNode('Stair%i' % i))
      np.node().addShape(shape)
      np.setPos(pos)
      np.setCollideMask(BitMask32.allOn())

      npV = loader.loadModel('models/box.egg')
      npV.reparentTo(np)
      npV.setScale(size)

      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
    center = Point3(0, 0, 0)
    radius = Vec3(1, 1, 1) * 1.5
    node = BulletSoftBodyNode.makeEllipsoid(info, center, radius, 128)
    node.setName('Ellipsoid')
    node.getMaterial(0).setLinearStiffness(0.1)
    node.getCfg().setDynamicFrictionCoefficient(1)
    node.getCfg().setDampingCoefficient(0.001)
    node.getCfg().setPressureCoefficient(1500)
    node.setTotalMass(30, True)
    node.setPose(True, False)

    np = self.worldNP.attachNewNode(node)
    np.setPos(15, 0, 12)
    #np.setH(90.0)
    #np.showBounds()
    #np.showTightBounds()
    self.world.attachSoftBody(np.node())

    geom = BulletHelper.makeGeomFromFaces(node)
    node.linkGeom(geom)
    nodeV = GeomNode('EllipsoidVisual')
    nodeV.addGeom(geom)
    npV = np.attachNewNode(nodeV)
Example #6
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, -2)
    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
    def makeSB(pos, hpr):

      import torus
      geom = torus.makeGeom()

      #geom = loader.loadModel('models/torus.egg') \
      #    .findAllMatches('**/+GeomNode').getPath(0).node() \
      #    .modifyGeom(0)

      geomNode = GeomNode('')
      geomNode.addGeom(geom)

      node = BulletSoftBodyNode.makeTriMesh(info, geom) 
      node.linkGeom(geomNode.modifyGeom(0))

      node.generateBendingConstraints(2)
      node.getCfg().setPositionsSolverIterations(2)
      node.getCfg().setCollisionFlag(BulletSoftBodyConfig.CFVertexFaceSoftSoft, True)
      node.randomizeConstraints()
      node.setTotalMass(50, True)

      softNP = self.worldNP.attachNewNode(node)
      softNP.setPos(pos)
      softNP.setHpr(hpr)
      self.world.attachSoftBody(node)

      geomNP = softNP.attachNewNode(geomNode)

    makeSB(Point3(-3, 0, 4), (0, 0, 0))
    makeSB(Point3(0, 0, 4), (0, 90, 90))
    makeSB(Point3(3, 0, 4), (0, 0, 0))
Example #7
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, -2)
        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
        def makeSB(pos, hpr):

            import torus
            geom = torus.makeGeom()

            #geom = loader.loadModel('models/torus.egg') \
            #    .findAllMatches('**/+GeomNode').getPath(0).node() \
            #    .modifyGeom(0)

            geomNode = GeomNode('')
            geomNode.addGeom(geom)

            node = BulletSoftBodyNode.makeTriMesh(info, geom)
            node.linkGeom(geomNode.modifyGeom(0))

            node.generateBendingConstraints(2)
            node.getCfg().setPositionsSolverIterations(2)
            node.getCfg().setCollisionFlag(
                BulletSoftBodyConfig.CFVertexFaceSoftSoft, True)
            node.randomizeConstraints()
            node.setTotalMass(50, True)

            softNP = self.worldNP.attachNewNode(node)
            softNP.setPos(pos)
            softNP.setHpr(hpr)
            self.world.attachSoftBody(node)

            geomNP = softNP.attachNewNode(geomNode)

        makeSB(Point3(-3, 0, 4), (0, 0, 0))
        makeSB(Point3(0, 0, 4), (0, 90, 90))
        makeSB(Point3(3, 0, 4), (0, 0, 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)
Example #9
0
	def addQuad(self, p1, p2, p3, p4):
		mesh = BulletTriangleMesh()
		mesh.addTriangle(p1, p2, p3)
		mesh.addTriangle(p1, p3, p4)
		shape = BulletTriangleMeshShape(mesh, dynamic=False)
		self.node.addShape(shape)
Example #10
0
 def addQuad(self, p1, p2, p3, p4):
     mesh = BulletTriangleMesh()
     mesh.addTriangle(p1, p2, p3)
     mesh.addTriangle(p1, p3, p4)
     shape = BulletTriangleMeshShape(mesh, dynamic=False)
     self.node.addShape(shape)
Example #11
0
  def setup(self):
    self.worldNP = render.attachNewNode('World')

    # World
    self.debugNP = self.worldNP.attachNewNode(BulletDebugNode('Debug'))
    self.debugNP.show()
    self.debugNP.node().showWireframe(True)
    self.debugNP.node().showConstraints(True)
    self.debugNP.node().showBoundingBoxes(False)
    self.debugNP.node().showNormals(True)

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

    # Plane (static)
    shape = BulletPlaneShape(Vec3(0, 0, 1), 0)

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

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

    # Box (dynamic)
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5))

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

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

    self.boxNP = np # For applying force & torque

    #np.node().notifyCollisions(True)
    #self.accept('bullet-contact-added', self.doAdded)
    #self.accept('bullet-contact-destroyed', self.doRemoved)

    # Sphere (dynamic)
    shape = BulletSphereShape(0.6)

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

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

    # Cone (dynamic)
    shape = BulletConeShape(0.6, 1.2, ZUp)

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

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

    # Capsule (dynamic)
    shape = BulletCapsuleShape(0.5, 1.0, ZUp)

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

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

    # Cyliner (dynamic)
    shape = BulletCylinderShape(0.7, 1.5, ZUp)

    np = self.worldNP.attachNewNode(BulletRigidBodyNode('Cylinder'))
    np.node().setMass(1.0)
    np.node().addShape(shape)
    np.setPos(4, 4, 4)
    np.setCollideMask(BitMask32.allOn())

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

    # Convex (dynamic)
    shape = BulletConvexHullShape()
    shape.addPoint(Point3(1, 1, 2))
    shape.addPoint(Point3(0, 0, 0))
    shape.addPoint(Point3(2, 0, 0))
    shape.addPoint(Point3(0, 2, 0))
    shape.addPoint(Point3(2, 2, 0))

    # Another way to create the convex hull shape:
    #shape = BulletConvexHullShape()
    #shape.addArray([
    #  Point3(1, 1, 2),
    #  Point3(0, 0, 0),
    #  Point3(2, 0, 0),
    #  Point3(0, 2, 0),
    #  Point3(2, 2, 0),
    #])

    # Yet another way to create the convex hull shape:
    #geom = loader.loadModel('models/box.egg')\
    #         .findAllMatches('**/+GeomNode')\
    #         .getPath(0)\
    #         .node()\
    #         .getGeom(0)
    #shape = BulletConvexHullShape()
    #shape.addGeom(geom)

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

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

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

    # Another way to create the triangle mesh shape:
    #geom = loader.loadModel('models/box.egg')\
    #         .findAllMatches('**/+GeomNode')\
    #         .getPath(0)\
    #         .node()\
    #         .getGeom(0)
    #mesh = BulletTriangleMesh()
    #mesh.addGeom(geom)
    #shape = BulletTriangleMeshShape(mesh, dynamic=False)

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

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

    # MutiSphere
    points = [Point3(-1, 0, 0), Point3(0, 0, 0), Point3(1, 0, 0)]
    radii = [.4, .8, .6]
    shape = BulletMultiSphereShape(points, radii)

    np = self.worldNP.attachNewNode(BulletRigidBodyNode('MultiSphere'))
    np.node().setMass(1.0)
    np.node().addShape(shape)
    np.setPos(8, 0, 4)
    np.setCollideMask(BitMask32.allOn())

    self.world.attachRigidBody(np.node())
Example #12
0
    def setup(self):
        self.worldNP = render.attachNewNode('World')

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

        #self.debugNP.showTightBounds()
        #self.debugNP.showBounds()

        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, -2)
        np.setCollideMask(BitMask32.allOn())

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

        # Stair
        origin = Point3(0, 0, 0)
        size = Vec3(2, 10, 1)
        shape = BulletBoxShape(size * 0.5)
        for i in range(10):
            pos = origin + size * i
            pos.setY(0)

            np = self.worldNP.attachNewNode(BulletRigidBodyNode('Stair%i' % i))
            np.node().addShape(shape)
            np.setPos(pos)
            np.setCollideMask(BitMask32.allOn())

            npV = loader.loadModel('models/box.egg')
            npV.reparentTo(np)
            npV.setScale(size)

            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
        center = Point3(0, 0, 0)
        radius = Vec3(1, 1, 1) * 1.5
        node = BulletSoftBodyNode.makeEllipsoid(info, center, radius, 128)
        node.setName('Ellipsoid')
        node.getMaterial(0).setLinearStiffness(0.1)
        node.getCfg().setDynamicFrictionCoefficient(1)
        node.getCfg().setDampingCoefficient(0.001)
        node.getCfg().setPressureCoefficient(1500)
        node.setTotalMass(30, True)
        node.setPose(True, False)

        np = self.worldNP.attachNewNode(node)
        np.setPos(15, 0, 12)
        #np.setH(90.0)
        #np.showBounds()
        #np.showTightBounds()
        self.world.attachSoftBody(np.node())

        geom = BulletHelper.makeGeomFromFaces(node)
        node.linkGeom(geom)
        nodeV = GeomNode('EllipsoidVisual')
        nodeV.addGeom(geom)
        npV = np.attachNewNode(nodeV)
    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)
    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, -2)
        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
        for i in range(50):
            p00 = Point3(-2, -2, 0)
            p10 = Point3(2, -2, 0)
            p01 = Point3(-2, 2, 0)
            p11 = Point3(2, 2, 0)
            node = BulletSoftBodyNode.makePatch(info, p00, p10, p01, p11, 6, 6,
                                                0, True)
            node.generateBendingConstraints(2)
            node.getCfg().setLiftCoefficient(0.004)
            node.getCfg().setDynamicFrictionCoefficient(0.0003)
            node.getCfg().setAeroModel(BulletSoftBodyConfig.AMVertexTwoSided)
            node.setTotalMass(0.1)
            node.addForce(Vec3(0, 2, 0), 0)

            np = self.worldNP.attachNewNode(node)
            np.setPos(self.Vec3Rand() * 10 + Vec3(0, 0, 20))
            np.setHpr(self.Vec3Rand() * 16)
            self.world.attachSoftBody(node)

            fmt = GeomVertexFormat.getV3n3t2()
            geom = BulletHelper.makeGeomFromFaces(node, fmt, True)
            node.linkGeom(geom)
            nodeV = GeomNode('')
            nodeV.addGeom(geom)
            npV = np.attachNewNode(nodeV)