예제 #1
0
    def create_ball_coll(self, ball_actor):
        # Build a collisionNode for this smiley which is a sphere of the same diameter as the model
        ball_coll_node = ball_actor.attachNewNode(CollisionNode('ball_cnode'))
        ball_coll_sphere = CollisionSphere(0, 0, 0, 1)
        ball_coll_node.node().addSolid(ball_coll_sphere)
        # Watch for collisions with our brothers, so we'll push out of each other
        ball_coll_node.node().setIntoCollideMask(BitMask32().bit(
            self.ball_bit))
        # we're only interested in colliding with the ground and other smileys
        cMask = BitMask32()
        cMask.setBit(self.ground_bit)
        cMask.setBit(self.ball_bit)
        ball_coll_node.node().setFromCollideMask(cMask)

        # Now, to keep the spheres out of the ground plane and each other, let's attach a physics handler to them
        ball_handler = PhysicsCollisionHandler()

        # Set the physics handler to manipulate the smiley actor's transform.
        ball_handler.addCollider(ball_coll_node, ball_actor)

        # This call adds the physics handler to the traverser list
        # (not related to last call to addCollider!)
        self.base.cTrav.addCollider(ball_coll_node, ball_handler)

        # Now, let's set the collision handler so that it will also do a CollisionHandlerEvent callback
        # But...wait?  Aren't we using a PhysicsCollisionHandler?
        # The reason why we can get away with this is that all CollisionHandlerXs are inherited from
        # CollisionHandlerEvent,
        # so all the pattern-matching event handling works, too
        ball_handler.addInPattern('%fn-into-%in')

        return ball_coll_node
예제 #2
0
 def __init__(self, debug=False):
     self._debug = debug
     
     self.handler = PhysicsCollisionHandler()
     self.handler.setStaticFrictionCoef(0.1)
     self.handler.setDynamicFrictionCoef(0.05)
     self.handler.addInPattern('into-%in')
     self.handler.addAgainPattern('again-%in')
     self.handler.addOutPattern('out-%in')
     self.handler.addInPattern('%fn-into-%in')
예제 #3
0
 def make_traverser_handler(self):
     base.cTrav = CollisionTraverser()
     #base.mchandler = CollisionHandlerEvent()
     #base.mchandler = CollisionHandlerPusher()
     base.mchandler = PhysicsCollisionHandler()
     base.mchandler.addInPattern('into-%in')
     base.mchandler.addAgainPattern('%fn-again-%in')
     base.mchandler.addOutPattern('out-%in')
     base.mchandler.setStaticFrictionCoef(0.91)
     base.mchandler.setDynamicFrictionCoef(0.91)
예제 #4
0
 def __init__(self, mouse):
     ShowBase.__init__(self)
     self.mouse = mouse
     self.joy_x = None
     self.joy_y = None
     props = WindowProperties()
     props.setMouseMode(WindowProperties.MRelative)  # keep mouse in screen
     self.disableMouse()
     self.win.requestProperties(props)
     self.setBackgroundColor(0, 0, 0)
     # Make missiles glow
     self.filters = CommonFilters(self.win, self.cam)
     self.filters.setBloom(blend=(0, 0, 0, 1), desat=-0.5, intensity=3.0, size="large")
     self.screen_width = self.win.getXSize()
     self.screen_height = self.win.getYSize()
     self.center_x = self.screen_width/2
     self.center_y = self.screen_height/2
     # self.win.movePointer(0, self.center_x, self.center_y)
     self.enableParticles()
     self.cTrav = CollisionTraverser()
     # self.cTrav.setRespectPrevTransform(True)
     self.pusher = PhysicsCollisionHandler()
     self.pusher.addInPattern('%fn-into-%in')
     self.target = None
     self.maxvel = 50
     self.roll_time = 0
     self.fuel = 1000
     self.ship()
     self.sounds()
     self.hud()
     self.part = Spacedust(self)
     self.events()
     self.camLens.setFov(70)
     self.camLens.setNear(1)
     self.camLens.setFar(500)
     self.get_key = {
         "ACCEL": False,
         "DECEL": False,
         "FORWARD_THRUST": False,
         "REVERSE_THRUST": False,
         "ROLL_LEFT": False,
         "ROLL_RIGHT": False,
         "ROLL_LEFT_BEG": False,
         "ROLL_RIGHT_BEG": False,
         "FIRE": False,
         "FIRING": False,
         "LOCK": False,
         "LOCKING": False,
     }
     self.AIworld = AIWorld(self.render)
     self.taskMgr.add(self.update, "task-update")
     self.taskMgr.doMethodLater(1, self.fuel_usage, "task-fuel-usage")
     self.taskMgr.add(self.AI_update, "AI-update")
     self.gen_enemy()
 def setupPhysics(self, name):
     an = ActorNode('%s-%s' % (name, self.doId))
     anp = NodePath(an)
     if not self.isEmpty():
         self.reparentTo(anp)
     NodePath.assign(self, anp)
     self.physicsObject = an.getPhysicsObject()
     self.setTag('object', str(self.doId))
     self.collisionNodePath.reparentTo(self)
     self.handler = PhysicsCollisionHandler()
     self.handler.addCollider(self.collisionNodePath, self)
     self.collideName = self.uniqueName('collide')
     self.handler.addInPattern(self.collideName + '-%in')
     self.handler.addAgainPattern(self.collideName + '-%in')
     self.watchDriftName = self.uniqueName('watchDrift')
예제 #6
0
    def setupCollision(self):
        base.cTrav = CollisionTraverser()
        self.collisionHandlerEvent = CollisionHandlerEvent()
        self.physicsCollisionPusher = PhysicsCollisionHandler()

        self.collisionHandlerEvent.addInPattern('into-%in')
        self.collisionHandlerEvent.addOutPattern('outof-%in')

        # create masks
        defaultCollisionMask = BitMask32(0b0001)  #0x1
        segmentCollisionMask = BitMask32(0b1000)  #0x8
        stairsCollisionMask = BitMask32(0b0010)  #0x2
        marioBodyCollisionMask = BitMask32(0b0011)  #0x3
        collisionWallsForBarrels = BitMask32(0b0100)  #0x4

        # mario segment collider
        ray = CollisionSegment(7, 0, -4.5, 7, 0, -5.1)
        cnodePath = self.mario.attachNewNode(CollisionNode('marioRay'))
        cnodePath.node().addSolid(ray)
        cnodePath.node().setFromCollideMask(segmentCollisionMask)
        cnodePath.node().setIntoCollideMask(0)
        cnodePath.show()
        base.cTrav.addCollider(cnodePath, self.collisionHandlerEvent)

        self.setupBoxCollider(self.mario, 7, 0, -4.5, 0.5, 5, 0.5,
                              'marioHitBox', self.collisionHandlerEvent,
                              marioBodyCollisionMask, marioBodyCollisionMask)

        stairs1 = self.scene.find("bottomstair")
        self.setupBoxCollider(stairs1, -6.8, 0, -3.0, 0.5, 5, 2.5,
                              'stairs1HitBox', self.collisionHandlerEvent,
                              stairsCollisionMask, stairsCollisionMask)

        stairs2 = self.scene.find("middlestair")
        self.setupBoxCollider(stairs2, -0.86, 0, .1, 0.5, 5, 2.1,
                              'stairs2HitBox', self.collisionHandlerEvent,
                              stairsCollisionMask, stairsCollisionMask)

        stairs3 = self.scene.find("topstair")
        self.setupBoxCollider(stairs3, -6.8, 0, 3.1, 0.5, 5, 2.2,
                              'stairs3HitBox', self.collisionHandlerEvent,
                              stairsCollisionMask, stairsCollisionMask)

        hammer = self.scene.find("MainGroup")  # hammer
        self.setupBoxCollider(hammer, 5.5, 0, -1.5, 0.5, 5, 0.5,
                              'hammer1HitBox', self.collisionHandlerEvent,
                              stairsCollisionMask, stairsCollisionMask)

        dk = self.scene.find("hammer1")
        self.setupBoxCollider(dk, 8.7, 0, 5, 1, 5, 1, 'dkHitBox',
                              self.collisionHandlerEvent, stairsCollisionMask,
                              stairsCollisionMask)

        floor0 = self.scene.find("floor0")
        self.setupBoxCollider(floor0,
                              -2.5,
                              0,
                              -5.5,
                              10,
                              5,
                              0.5,
                              'floor0HitBox',
                              self.collisionHandlerEvent,
                              intoCollisionMask=segmentCollisionMask)

        floor1 = self.scene.find("floor1")
        self.setupBoxCollider(floor1,
                              2,
                              0,
                              -2.5,
                              8.4,
                              5,
                              0.5,
                              'floor1HitBox',
                              self.collisionHandlerEvent,
                              intoCollisionMask=segmentCollisionMask)
        floor2_1 = self.scene.find("floor2")
        self.setupBoxCollider(floor2_1,
                              3.6,
                              0,
                              0.5,
                              3.8,
                              5,
                              0.5,
                              'floor21HitBox',
                              self.collisionHandlerEvent,
                              intoCollisionMask=segmentCollisionMask)
        floor2_2 = self.scene.find("pCube4")
        self.setupBoxCollider(floor2_2,
                              -6.3,
                              0,
                              0.5,
                              5.0,
                              5,
                              0.5,
                              'floor22HitBox',
                              self.collisionHandlerEvent,
                              intoCollisionMask=segmentCollisionMask)
        floor3 = self.scene.find("floors")
        self.setupBoxCollider(floor3,
                              1.8,
                              0,
                              3.5,
                              8,
                              5,
                              0.5,
                              'floor3HitBox',
                              self.collisionHandlerEvent,
                              intoCollisionMask=segmentCollisionMask)

        rightWall = self.scene.find("rightWall")
        self.setupBoxCollider(rightWall,
                              -12,
                              0,
                              0,
                              1,
                              5,
                              10,
                              'rightWallHitBox',
                              self.collisionHandlerEvent,
                              fromCollisionMask=collisionWallsForBarrels,
                              intoCollisionMask=collisionWallsForBarrels)

        leftWall = self.scene.find("walls")
        self.setupBoxCollider(leftWall,
                              11.5,
                              0,
                              0,
                              1,
                              5,
                              10,
                              'leftWallHitBox',
                              self.collisionHandlerEvent,
                              fromCollisionMask=collisionWallsForBarrels,
                              intoCollisionMask=collisionWallsForBarrels)

        barrelFixer = self.scene.attachNewNode("barrelFixer")
        self.setupBoxCollider(barrelFixer,
                              -3,
                              0,
                              0.505,
                              10,
                              5,
                              0.5,
                              'barrelFixerHitBox',
                              self.collisionHandlerEvent,
                              fromCollisionMask=collisionWallsForBarrels,
                              intoCollisionMask=collisionWallsForBarrels)

        barrelDestroyer = self.scene.attachNewNode("barrelDestroyer")
        self.setupBoxCollider(barrelDestroyer,
                              0,
                              0,
                              -8,
                              15,
                              5,
                              0.5,
                              'barrelDestroyerHitBox',
                              self.collisionHandlerEvent,
                              fromCollisionMask=collisionWallsForBarrels,
                              intoCollisionMask=collisionWallsForBarrels)

        self.accept('into-stairs1HitBox', self.enableStair)
        self.accept('outof-stairs1HitBox', self.disableStair)
        self.accept('into-stairs2HitBox', self.enableStair)
        self.accept('outof-stairs2HitBox', self.disableStair)
        self.accept('into-stairs3HitBox', self.enableStair)
        self.accept('outof-stairs3HitBox', self.disableStair)
        self.accept('into-hammer1HitBox', self.enableHammer)
        self.accept('into-dkHitBox', self.dkArrived)

        self.accept('into-floor0HitBox', self.enableJump)
        self.accept('outof-floor0HitBox', self.disableJump)
        self.accept('into-floor1HitBox', self.enableJump)
        self.accept('outof-floor1HitBox', self.disableJump)
        self.accept('into-floor21HitBox', self.enableJump)
        self.accept('outof-floor21HitBox', self.disableJump)
        self.accept('into-floor22HitBox', self.enableJump)
        self.accept('outof-floor22HitBox', self.disableJump)
        self.accept('into-floor3HitBox', self.enableJump)
        self.accept('outof-floor3HitBox', self.disableJump)

        self.accept("into-barrelCollider", self.barrelCrash)

        base.cTrav.showCollisions(self.render)
예제 #7
0
)
snipstuff.info_show()

#=========================================================================
# Main
""" As you probably know, physics in games is a cool feature that give life to the show and today it is quite impossible not to face it while developing a modern game and panda3D make no exception. Though, you got to know that panda3d physics engine is not a complete physics engine but is there to solve simple tasks, especially related to particle management, even if could be applied to 3d models as well, so you may first understand what it got to offer and what are its limitations then see if suits for your game. We're not going to dig furter this issue, cos the panda3d manual already gotta dedicated section but rather we'll see how to setup collisions to interact with physics. We got a simple scene with a roaming smiley and a shower of frowneys and watermelons where each element in the scene will be able to collide each other, floor included and everything driven by the panda3d physics engine.
Note that we don't need to set masking in this setup cos each objet collide with each other, therefore every collider, beside the floor, gotta act as from and to object therefore the default masking setup is good as is.
"""
#=========================================================================

#** Collision system ignition - even if we're going to interact with the physics routines, the usual traverser is always in charge to drive collisions
base.cTrav = CollisionTraverser()
# look here: we enable the particle system - this is the evidence of what I was saying above, because the panda physics engine is conceived mainly to manage particles.
base.enableParticles()
# here there is the handler to use this time to manage collisions.
collisionHandler = PhysicsCollisionHandler()

#** This is the first time we see this collider: it is used mainly to define a flat infinite plane surface -
# it is very reliable and stable and gives high fame rate performances so use it as much as you can.
# Note that we specify 2 corners of the plane of just 1 unit per side but this collider will collide
#  with everithing as it was an infinite plane anyhow. Note that there is no need to set anything further for it.

cp = CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0)))
planeNP = base.render.attachNewNode(CollisionNode('planecnode'))
planeNP.node().addSolid(cp)
planeNP.show()

#** This is how to define the gravity force to make our stuff fall down: first off we define a ForceNode and attach to the render, so that everything below will be affected by this force
globalforcesFN = ForceNode('world-forces')
globalforcesFNP = base.render.attachNewNode(globalforcesFN)
# then we set a linear force that will act in Z axis-drag-down-force of 9.81 units per second.