def __setupCollisions(self):
        self.cTrav = CollisionTraverser("traverser")
        base.cTrav = self.cTrav

        self.physicsCollisionHandler = PhysicsCollisionHandler()
        self.physicsCollisionHandler.setDynamicFrictionCoef(0.5)
        self.physicsCollisionHandler.setStaticFrictionCoef(0.7)
Exemplo n.º 2
0
    def __init__(self, theavatar, floor, walls=None):
        # Enables the built-in physics
        base.enableParticles()

        avatar = theavatar
        # This is needed so we don't calculate collisions on the actual model,
        # but on the collision node we will add later
        avatar.setCollideMask(BitMask32.allOff())
        # Whenever we want to inter-act with anything to do with collisions/physics,
        # we want to use the actor node path
        self.avatarNP = render.attachNewNode(ActorNode("actor"))
        # Sets up the mass. Note that in this scenario, mass is not taken into consideration.
        self.avatarNP.node().getPhysicsObject().setMass(100.)
        # Parent our avatar to the ready to go physics node
        avatar.reparentTo(self.avatarNP)

        # Set up the gravity force
        gravityFN = ForceNode('world-forces')
        gravityFNP = render.attachNewNode(gravityFN)
        gravityForce = LinearVectorForce(0, 0, -9.81)
        gravityForce.setMassDependent(False)
        gravityFN.addForce(gravityForce)
        # Attach it to the global physics manager
        base.physicsMgr.addLinearForce(gravityForce)

        # Set the collision traverser
        base.cTrav = CollisionTraverser()
        base.cTrav.showCollisions(base.render)
        #define 2 masks, one for the floor contacts between the avatar model and the collision surfaces
        mask_floor = BitMask32.bit(1)
        mask_walls = BitMask32.bit(2)

        # Assign the floor collsion mask to the floor model and hide it
        self.floor = floor
        self.floor.setCollideMask(mask_floor)
        self.floor.hide()

        # the fromObject is our collision information. The documentation for Panda3d
        # recommends that we use a sphere to handle collisions.
        fromObject = self.avatarNP.attachNewNode(
            CollisionNode("agentCollisionNode"))
        fromObject.node().addSolid(CollisionSphere(0, 0, 2.5, 2.5))
        # We want to handle any sort of collision that happens to us, but not
        # vice-versa
        fromObject.node().setFromCollideMask(mask_floor)
        fromObject.node().setIntoCollideMask(BitMask32.allOff())
        # show the collision shpere
        fromObject.show()

        # Create a collision handler to handle all the physics
        pusher = PhysicsCollisionHandler()
        # attach it our collision node along with our actor node
        pusher.addCollider(fromObject, self.avatarNP)
        # Add the handler to the main collision traverser
        base.cTrav.addCollider(fromObject, pusher)

        # Tell the global physicsMgr about our actor node
        base.physicsMgr.attachPhysicalNode(self.avatarNP.node())
        #
        self.avatarNP.setZ(.2)