Exemple #1
0
    def _setup(self):
        if hasattr(self, '_model_path'):
            # Collidable objects
            visNP = loader.loadModel(self._model_path)
            visNP.clearModelNodes()
            visNP.reparentTo(render)
            pos = (0., 0., 0.)
            visNP.setPos(pos[0], pos[1], pos[2])

            bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
            for bodyNP in bodyNPs:
                bodyNP.reparentTo(render)
                bodyNP.setPos(pos[0], pos[1], pos[2])

                if isinstance(bodyNP.node(), BulletRigidBodyNode):
                    bodyNP.node().setMass(0.0)
                    bodyNP.node().setKinematic(True)
                    bodyNP.setCollideMask(BitMask32.allOn())
                    self._world.attachRigidBody(bodyNP.node())
        else:
            ground = self._worldNP.attachNewNode(BulletRigidBodyNode('Ground'))
            shape = BulletPlaneShape(Vec3(0, 0, 1), 0)
            ground.node().addShape(shape)
            ground.setCollideMask(BitMask32.allOn())
            self._world.attachRigidBody(ground.node())
        self._place_vehicle()
        self._setup_light()
        self._setup_restart_pos()
 def _setup_collision_object(self,
                             path,
                             pos=(0.0, 0.0, 0.0),
                             hpr=(0.0, 0.0, 0.0),
                             scale=1):
     visNP = loader.loadModel(path)
     visNP.clearModelNodes()
     visNP.reparentTo(render)
     visNP.setPos(pos[0], pos[1], pos[2])
     visNP.setHpr(hpr[0], hpr[1], hpr[2])
     visNP.set_scale(scale, scale, scale)
     bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
     for bodyNP in bodyNPs:
         bodyNP.reparentTo(render)
         if not ('wall' in bodyNP.name or 'ground' in bodyNP.name):
             bodyNP.setPos(pos[0], pos[1], pos[2] + 0.3)
         else:
             bodyNP.setPos(pos[0], pos[1], pos[2])
         bodyNP.setHpr(hpr[0], hpr[1], hpr[2])
         bodyNP.set_scale(scale, scale, scale)
         if isinstance(bodyNP.node(), BulletRigidBodyNode):
             bodyNP.node().setMass(0.0)
             bodyNP.node().setKinematic(True)
             bodyNP.setCollideMask(BitMask32.allOn())
             self._world.attachRigidBody(bodyNP.node())
         else:
             print("Issue")
  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)
Exemple #4
0
    def setup(self):
        self.worldNP = render.attachNewNode('World')

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

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

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

        # collision
        visNP = loader.loadModel('../models/coryf2.egg')
        visNP.clearModelNodes()
        visNP.reparentTo(render)
        pos = (7., 60.0, 3.8)
        visNP.setPos(pos[0], pos[1], pos[2])

        bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
        for bodyNP in bodyNPs:
            bodyNP.reparentTo(render)
            bodyNP.setPos(pos[0], pos[1], pos[2])

            if isinstance(bodyNP.node(), BulletRigidBodyNode):
                bodyNP.node().setMass(0.0)
                bodyNP.node().setKinematic(True)
                bodyNP.setCollideMask(BitMask32.allOn())
                self.world.attachRigidBody(bodyNP.node())
Exemple #5
0
    def setup(self):
        self.worldNP = render.attachNewNode('World')

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

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

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

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

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

        # collision
        self.maze = []
        for pos in [(0.0, 72.0, 0.0), (-11.0, 60.0, 0.0), (11.0, 60.0, 0.0),
                    (-11.0, 48.0, 0.0), (11.0, 48.0, 0.0), (-11.0, 36.0, 0.0),
                    (11.0, 36.0, 0.0), (-11.0, 24.0, 0.0), (11.0, 24.0, 0.0),
                    (-11.0, 12.0, 0.0), (11.0, 12.0, 0.0), (-11.0, 0.0, 0.0),
                    (11.0, 0.0, 0.0), (0.0, -12.0, 0.0), (0.5, 12.0, 1.0),
                    (-0.5, 12.0, 1.0)]:
            translate = False
            if (abs(pos[0]) == 0.5):
                translate = True
                visNP = loader.loadModel('../models/ball.egg')
            else:
                visNP = loader.loadModel('../models/maze.egg')
            visNP.clearModelNodes()
            visNP.reparentTo(self.ground)
            visNP.setPos(pos[0], pos[1], pos[2])

            bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
            for bodyNP in bodyNPs:
                bodyNP.reparentTo(render)
                if translate:
                    bodyNP.setPos(pos[0], pos[1], pos[2] - 1)
                else:
                    bodyNP.setPos(pos[0], pos[1], pos[2])

                if isinstance(bodyNP.node(), BulletRigidBodyNode):
                    bodyNP.node().setMass(0.0)
                    bodyNP.node().setKinematic(True)
                    self.maze.append(bodyNP)

        for bodyNP in self.maze:
            self.world.attachRigidBody(bodyNP.node())
Exemple #6
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
Exemple #7
0
    def setup(self):
        self.worldNP = render.attachNewNode('World')

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

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

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

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

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

        # collision
        self.maze = []
        for pos in [(0.0, 72.0, 0.0), (-11.0, 60.0, 0.0), (11.0, 60.0, 0.0),
                    (-11.0, 48.0, 0.0), (11.0, 48.0, 0.0), (-11.0, 36.0, 0.0),
                    (11.0, 36.0, 0.0), (-11.0, 24.0, 0.0), (11.0, 24.0, 0.0),
                    (-11.0, 12.0, 0.0), (11.0, 12.0, 0.0), (-11.0, 0.0, 0.0),
                    (11.0, 0.0, 0.0), (0.0, -12.0, 0.0), (0.5, 12.0, 1.0),
                    (-0.5, 12.0, 1.0)]:
            translate = False
            if (abs(pos[0]) == 0.5):
                translate = True
                visNP = loader.loadModel('../models/ball.egg')
            else:
                visNP = loader.loadModel('../models/maze.egg')
            visNP.clearModelNodes()
            visNP.reparentTo(self.ground)
            visNP.setPos(pos[0], pos[1], pos[2])

            bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
            for bodyNP in bodyNPs:
                bodyNP.reparentTo(render)
                if translate:
                    bodyNP.setPos(pos[0], pos[1], pos[2] - 1)
                else:
                    bodyNP.setPos(pos[0], pos[1], pos[2])

                if isinstance(bodyNP.node(), BulletRigidBodyNode):
                    bodyNP.node().setMass(0.0)
                    bodyNP.node().setKinematic(True)
                    self.maze.append(bodyNP)

        for bodyNP in self.maze:
            self.world.attachRigidBody(bodyNP.node())
        # Chassis
        mass = rospy.get_param('~mass')
        #chassis_shape = rospy.get_param('~chassis_shape')
        shape = BulletBoxShape(Vec3(0.6, 1.4, 0.5))
        ts = TransformState.makePos(Point3(0, 0, 0.5))

        np = self.worldNP.attachNewNode(BulletRigidBodyNode('Vehicle'))
        np.node().addShape(shape, ts)
        rand_vals = numpy.random.random(2) * 8 - 4.0
        np.setPos(rand_vals[0], 0.0, -0.6)
        np.node().setMass(mass)
        np.node().setDeactivationEnabled(False)

        first_person = rospy.get_param('~first_person')
        self.camera_sensor = Panda3dCameraSensor(base,
                                                 color=True,
                                                 depth=True,
                                                 size=(160, 90))

        self.camera_node = self.camera_sensor.cam
        if first_person:
            self.camera_node.reparentTo(np)
            self.camera_node.setPos(0.0, 1.0, 1.0)
            self.camera_node.lookAt(0.0, 6.0, 0.0)
        else:
            self.camera_node.reparentTo(np)
            self.camera_node.setPos(0.0, -10.0, 5.0)
            self.camera_node.lookAt(0.0, 5.0, 0.0)
        base.cam.reparentTo(np)
        base.cam.setPos(0.0, -10.0, 5.0)
        base.cam.lookAt(0.0, 5.0, 0.0)
        self.world.attachRigidBody(np.node())

        np.node().setCcdSweptSphereRadius(1.0)
        np.node().setCcdMotionThreshold(1e-7)

        # Vehicle
        self.vehicle_node = np.node()
        self.vehicle = BulletVehicle(self.world, np.node())
        self.vehicle.setCoordinateSystem(ZUp)
        self.world.attachVehicle(self.vehicle)

        self.yugoNP = loader.loadModel('../models/yugo/yugo.egg')
        self.yugoNP.reparentTo(np)

        # Right front wheel
        np = loader.loadModel('../models/yugo/yugotireR.egg')
        np.reparentTo(self.worldNP)
        self.addWheel(Point3(0.70, 1.05, 0.3), True, np)

        # Left front wheel
        np = loader.loadModel('../models/yugo/yugotireL.egg')
        np.reparentTo(self.worldNP)
        self.addWheel(Point3(-0.70, 1.05, 0.3), True, np)

        # Right rear wheel
        np = loader.loadModel('../models/yugo/yugotireR.egg')
        np.reparentTo(self.worldNP)
        self.addWheel(Point3(0.70, -1.05, 0.3), False, np)

        # Left rear wheel
        np = loader.loadModel('../models/yugo/yugotireL.egg')
        np.reparentTo(self.worldNP)
        self.addWheel(Point3(-0.70, -1.05, 0.3), False, np)
    def __init__(self):
        ShowBase.__init__(self)
        base.set_background_color(0.1, 0.1, 0.8, 1)
        base.set_frame_rate_meter(True)

        base.cam.set_pos_hpr(0, 0, 25, 0, -90, 0)
        base.disable_mouse()

        # Input
        self.accept('escape', self.exitGame)
        self.accept('f1', base.toggle_wireframe)
        self.accept('f2', base.toggle_texture)
        self.accept('f3', self.toggle_debug)
        self.accept('f5', self.do_screenshot)

        # Setup scene 1: World
        self.debugNP = render.attach_new_node(BulletDebugNode('Debug'))
        self.debugNP.node().show_wireframe(True)
        self.debugNP.node().show_constraints(True)
        self.debugNP.node().show_bounding_boxes(True)
        self.debugNP.node().show_normals(True)
        self.debugNP.show()

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

        # Setup scene 2: Ball
        #visNP = loader.load_model('models/ball.egg')
        visNP = loader.load_model('samples/ball-in-maze/models/ball.egg.pz')
        visNP.clear_model_nodes()

        bodyNPs = BulletHelper.from_collision_solids(visNP, True)
        self.ballNP = bodyNPs[0]
        self.ballNP.reparent_to(render)
        self.ballNP.node().set_mass(1.0)
        self.ballNP.set_pos(4, -4, 1)
        self.ballNP.node().set_deactivation_enabled(False)

        visNP.reparent_to(self.ballNP)

        # Setup scene 3: Maze
        visNP = loader.load_model('models/maze.egg')
        #visNP = loader.load_model('samples/ball-in-maze/models/maze.egg.pz')
        visNP.clear_model_nodes()
        visNP.reparent_to(render)

        self.holes = []
        self.maze = []
        self.mazeNP = visNP

        bodyNPs = BulletHelper.from_collision_solids(visNP, True);
        for bodyNP in bodyNPs:
            bodyNP.reparent_to(render)

            if isinstance(bodyNP.node(), BulletRigidBodyNode):
                bodyNP.node().set_mass(0.0)
                bodyNP.node().set_kinematic(True)
                self.maze.append(bodyNP)

            elif isinstance(bodyNP.node(), BulletGhostNode):
                self.holes.append(bodyNP)

        # Lighting and material for the ball
        ambientLight = AmbientLight('ambientLight')
        ambientLight.set_color(LVector4(0.55, 0.55, 0.55, 1))
        directionalLight = DirectionalLight('directionalLight')
        directionalLight.set_direction(LVector3(0, 0, -1))
        directionalLight.set_color(LVector4(0.375, 0.375, 0.375, 1))
        directionalLight.set_specular_color(LVector4(1, 1, 1, 1))
        self.ballNP.set_light(render.attach_new_node(ambientLight))
        self.ballNP.set_light(render.attach_new_node(directionalLight))

        m = Material()
        m.set_specular(LVector4(1,1,1,1))
        m.set_shininess(96)
        self.ballNP.set_material(m, 1)

        # Startup
        self.start_game()
Exemple #9
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())

        # Box
        shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5) * 2.0)

        boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
        boxNP.node().setMass(150.0)
        boxNP.node().addShape(shape)
        boxNP.setPos(0, 0, 2)
        boxNP.setCollideMask(BitMask32.allOn())

        self.world.attachRigidBody(boxNP.node())

        visualNP = loader.loadModel('models/box.egg')
        visualNP.clearModelNodes()
        visualNP.setScale(2.0)
        visualNP.reparentTo(boxNP)

        # 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
        nx = 31
        ny = 31

        p00 = Point3(-8, -8, 0)
        p10 = Point3(8, -8, 0)
        p01 = Point3(-8, 8, 0)
        p11 = Point3(8, 8, 0)
        bodyNode = BulletSoftBodyNode.makePatch(info, p00, p10, p01, p11, nx,
                                                ny, 1 + 2 + 4 + 8, True)

        material = bodyNode.appendMaterial()
        material.setLinearStiffness(0.4)
        bodyNode.generateBendingConstraints(2, material)
        bodyNode.setTotalMass(50.0)
        bodyNode.getShape(0).setMargin(0.5)

        bodyNP = self.worldNP.attachNewNode(bodyNode)
        self.world.attachSoftBody(bodyNode)

        # Rendering with Geom:
        fmt = GeomVertexFormat.getV3n3t2()
        geom = BulletHelper.makeGeomFromFaces(bodyNode, fmt, True)
        bodyNode.linkGeom(geom)
        visNode = GeomNode('')
        visNode.addGeom(geom)
        visNP = bodyNP.attachNewNode(visNode)

        # Now we want to have a texture and texture coordinates.
        # The geom's format has already a column for texcoords, so we just need
        # to write texcoords using a GeomVertexRewriter.
        tex = loader.loadTexture('models/panda.jpg')
        visNP.setTexture(tex)
        BulletHelper.makeTexcoordsForPatch(geom, nx, ny)
  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)
Exemple #11
0
    def __init__(self):
        base.setBackgroundColor(0.1, 0.1, 0.8, 1)
        base.setFrameRateMeter(True)

        base.cam.setPosHpr(0, 0, 25, 0, -90, 0)
        base.disableMouse()

        # Input
        self.accept('escape', self.exitGame)
        self.accept('f1', self.toggleWireframe)
        self.accept('f2', self.toggleTexture)
        self.accept('f3', self.toggleDebug)
        self.accept('f5', self.doScreenshot)

        # Setup scene 1: World
        self.debugNP = render.attachNewNode(BulletDebugNode('Debug'))
        self.debugNP.node().showWireframe(True)
        self.debugNP.node().showConstraints(True)
        self.debugNP.node().showBoundingBoxes(True)
        self.debugNP.node().showNormals(True)
        self.debugNP.show()

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

        # Setup scene 2: Ball
        visNP = loader.loadModel('models/ball.egg')
        visNP.clearModelNodes()

        bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
        self.ballNP = bodyNPs[0]
        self.ballNP.reparentTo(render)
        self.ballNP.node().setMass(1.0)
        self.ballNP.setPos(4, -4, 1)
        self.ballNP.node().setDeactivationEnabled(False)

        visNP.reparentTo(self.ballNP)

        # Setup scene 3: Maze
        visNP = loader.loadModel('models/maze.egg')
        visNP.clearModelNodes()
        visNP.reparentTo(render)

        self.holes = []
        self.maze = []
        self.mazeNP = visNP

        bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
        for bodyNP in bodyNPs:
            bodyNP.reparentTo(render)

            if isinstance(bodyNP.node(), BulletRigidBodyNode):
                bodyNP.node().setMass(0.0)
                bodyNP.node().setKinematic(True)
                self.maze.append(bodyNP)

            elif isinstance(bodyNP.node(), BulletGhostNode):
                self.holes.append(bodyNP)

        # Lighting and material for the ball
        ambientLight = AmbientLight('ambientLight')
        ambientLight.setColor(Vec4(0.55, 0.55, 0.55, 1))
        directionalLight = DirectionalLight('directionalLight')
        directionalLight.setDirection(Vec3(0, 0, -1))
        directionalLight.setColor(Vec4(0.375, 0.375, 0.375, 1))
        directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
        self.ballNP.setLight(render.attachNewNode(ambientLight))
        self.ballNP.setLight(render.attachNewNode(directionalLight))

        m = Material()
        m.setSpecular(Vec4(1, 1, 1, 1))
        m.setShininess(96)
        self.ballNP.setMaterial(m, 1)

        # Startup
        self.startGame()
    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)
Exemple #13
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())

    # Box
    shape = BulletBoxShape(Vec3(0.5, 0.5, 0.5) * 2.0)

    boxNP = self.worldNP.attachNewNode(BulletRigidBodyNode('Box'))
    boxNP.node().setMass(150.0)
    boxNP.node().addShape(shape)
    boxNP.setPos(0, 0, 2)
    boxNP.setCollideMask(BitMask32.allOn())

    self.world.attachRigidBody(boxNP.node())

    visualNP = loader.loadModel('models/box.egg')
    visualNP.clearModelNodes()
    visualNP.setScale(2.0)
    visualNP.reparentTo(boxNP)

    # 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
    nx = 31
    ny = 31

    p00 = Point3(-8, -8, 0)
    p10 = Point3( 8, -8, 0)
    p01 = Point3(-8,  8, 0)
    p11 = Point3( 8,  8, 0)
    bodyNode = BulletSoftBodyNode.makePatch(info, p00, p10, p01, p11, nx, ny, 1+2+4+8, True) 

    material = bodyNode.appendMaterial()
    material.setLinearStiffness(0.4)
    bodyNode.generateBendingConstraints(2, material);
    bodyNode.setTotalMass(50.0)
    bodyNode.getShape(0).setMargin(0.5)

    bodyNP = self.worldNP.attachNewNode(bodyNode)
    self.world.attachSoftBody(bodyNode)

    # Rendering with Geom:
    fmt = GeomVertexFormat.getV3n3t2()
    geom = BulletHelper.makeGeomFromFaces(bodyNode, fmt, True)
    bodyNode.linkGeom(geom)
    visNode = GeomNode('')
    visNode.addGeom(geom)
    visNP = bodyNP.attachNewNode(visNode)

    # Now we want to have a texture and texture coordinates.
    # The geom's format has already a column for texcoords, so we just need
    # to write texcoords using a GeomVertexRewriter.
    tex = loader.loadTexture('models/panda.jpg')
    visNP.setTexture(tex)
    BulletHelper.makeTexcoordsForPatch(geom, nx, ny)
Exemple #15
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, -2)
        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
        for i in range(50):
            p00 = LPoint3(-2, -2, 0)
            p10 = LPoint3(2, -2, 0)
            p01 = LPoint3(-2, 2, 0)
            p11 = LPoint3(2, 2, 0)
            node = BulletSoftBodyNode.make_patch(info, p00, p10, p01, p11, 6,
                                                 6, 0, True)
            node.generate_bending_constraints(2)
            node.get_cfg().set_lift_coefficient(0.004)
            node.get_cfg().set_dynamic_friction_coefficient(0.0003)
            node.get_cfg().set_aero_model(
                BulletSoftBodyConfig.AM_vertex_two_sided)
            node.set_total_mass(0.1)
            node.add_force(LVector3(0, 2, 0), 0)

            np = self.worldNP.attach_new_node(node)
            np.set_pos(self.LVector3_rand() * 10 + LVector3(0, 0, 20))
            np.set_hpr(self.LVector3_rand() * 16)
            self.world.attach(node)

            fmt = GeomVertexFormat.get_v3n3t2()
            geom = BulletHelper.make_geom_from_faces(node, fmt, True)
            node.link_geom(geom)
            nodeV = GeomNode('')
            nodeV.add_geom(geom)
            npV = np.attach_new_node(nodeV)

            tex = loader.load_texture('models/panda.jpg')
            npV.set_texture(tex)
            BulletHelper.make_texcoords_for_patch(geom, 6, 6)
  def __init__(self):
    base.setBackgroundColor(0.1, 0.1, 0.8, 1)
    base.setFrameRateMeter(True)

    base.cam.setPosHpr(0, 0, 25, 0, -90, 0)
    base.disableMouse()

    # Input
    self.accept('escape', self.exitGame)
    self.accept('f1', self.toggleWireframe)
    self.accept('f2', self.toggleTexture)
    self.accept('f3', self.toggleDebug)
    self.accept('f5', self.doScreenshot)

    # Setup scene 1: World
    self.debugNP = render.attachNewNode(BulletDebugNode('Debug'))
    self.debugNP.node().showWireframe(True)
    self.debugNP.node().showConstraints(True)
    self.debugNP.node().showBoundingBoxes(True)
    self.debugNP.node().showNormals(True)
    self.debugNP.show()

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

    # Setup scene 2: Ball
    visNP = loader.loadModel('models/ball.egg')
    visNP.clearModelNodes()

    bodyNPs = BulletHelper.fromCollisionSolids(visNP, True)
    self.ballNP = bodyNPs[0]
    self.ballNP.reparentTo(render)
    self.ballNP.node().setMass(1.0)
    self.ballNP.setPos(4, -4, 1)
    self.ballNP.node().setDeactivationEnabled(False)

    visNP.reparentTo(self.ballNP)

    # Setup scene 3: Maze
    visNP = loader.loadModel('models/maze.egg')
    visNP.clearModelNodes()
    visNP.reparentTo(render)

    self.holes = []
    self.maze = []
    self.mazeNP = visNP

    bodyNPs = BulletHelper.fromCollisionSolids(visNP, True);
    for bodyNP in bodyNPs:
      bodyNP.reparentTo(render)

      if isinstance(bodyNP.node(), BulletRigidBodyNode):
        bodyNP.node().setMass(0.0)
        bodyNP.node().setKinematic(True)
        self.maze.append(bodyNP)

      elif isinstance(bodyNP.node(), BulletGhostNode):
        self.holes.append(bodyNP)

    # Lighting and material for the ball
    ambientLight = AmbientLight('ambientLight')
    ambientLight.setColor(Vec4(0.55, 0.55, 0.55, 1))
    directionalLight = DirectionalLight('directionalLight')
    directionalLight.setDirection(Vec3(0, 0, -1))
    directionalLight.setColor(Vec4(0.375, 0.375, 0.375, 1))
    directionalLight.setSpecularColor(Vec4(1, 1, 1, 1))
    self.ballNP.setLight(render.attachNewNode(ambientLight))
    self.ballNP.setLight(render.attachNewNode(directionalLight))

    m = Material()
    m.setSpecular(Vec4(1,1,1,1))
    m.setShininess(96)
    self.ballNP.setMaterial(m, 1)

    # Startup
    self.startGame()
    def setup(self):
        self.worldNP = render.attach_new_node('World')

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

        #self.debugNP.show_tight_bounds()
        #self.debugNP.show_bounds()

        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, -2)
        np.set_collide_mask(BitMask32.all_on())

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

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

            np = self.worldNP.attach_new_node(
                BulletRigidBodyNode('Stair{}'.format(i)))
            np.node().add_shape(shape)
            np.set_pos(pos)
            np.set_collide_mask(BitMask32.all_on())

            npV = loader.load_model('models/box.egg')
            npV.reparent_to(np)
            npV.set_scale(size)

            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
        center = LPoint3(0, 0, 0)
        radius = LVector3(1, 1, 1) * 1.5
        node = BulletSoftBodyNode.make_ellipsoid(info, center, radius, 128)
        node.set_name('Ellipsoid')
        node.get_material(0).set_linear_stiffness(0.1)
        node.get_cfg().set_dynamic_friction_coefficient(1)
        node.get_cfg().set_damping_coefficient(0.001)
        node.get_cfg().set_pressure_coefficient(1500)
        node.set_total_mass(30, True)
        node.set_pose(True, False)

        np = self.worldNP.attach_new_node(node)
        np.set_pos(15, 0, 12)
        #np.setH(90.0)
        #np.show_bounds()
        #np.show_tight_bounds()
        self.world.attach(np.node())

        geom = BulletHelper.make_geom_from_faces(node)
        node.link_geom(geom)
        nodeV = GeomNode('EllipsoidVisual')
        nodeV.add_geom(geom)
        npV = np.attach_new_node(nodeV)