def __init__(self): # start Panda3d base = ShowBase() # CollisionTraverser instance must be attached to base, # and must be called cTrav base.cTrav = CollisionTraverser() self.cHand = CollisionHandlerEvent() self.cHand.addInPattern('into-%in') # load smiley, which we will control # notice this is a little trickery. If we parent everything except smiley # to the camera, and the player is controlling the camera, then everything # stays fixed in the same place relative to the camera, and when the camera # moves, the player is given the illusion that he is moving smiley, rather than # the camera and the rest of the scene. self.smiley = base.loader.loadModel('smiley') self.smiley.reparentTo(render) self.smiley.setName('Smiley') # Setup a collision solid for this model. Our initCollisionSphere method returns # the cNodepath and the string representing it. We will use the cNodepath for # adding to the traverser, and the string to accept it for event handling. #sColl = self.initCollisionSphere(self.smiley) sColl = self.initCollisionSphere(self.smiley, True) print 'smiley', sColl[0] print 'smiley', sColl[1] # Add this object to the traverser. base.cTrav.addCollider(sColl[0], self.cHand) # load frowney self.frowney = base.loader.loadModel('frowney') self.frowney.setName('Frowney') self.frowney.reparentTo(camera) # Setup a collision solid for this model. fColl = self.initCollisionSphere(self.frowney, True) #print 'frowney', fColl[0] #print 'frowney', fColl[1] # Add this object to the traverser. base.cTrav.addCollider(fColl[0], self.cHand) # Accept the events sent by the collisions. # smiley is the object we are 'moving', so by convention # it is the from object. # If we instead put smiley as the into object here, # it would work fine, but then smiley would disappear # when we collided them. self.accept('into-' + fColl[1], self.collide) # set the initial positions of smiley and frowney self.set_initial_positions()
snipstuff.info.append("Simple Mouse Picking") snipstuff.info.append("a minimal sample to show a simple collision setup for picking an object with the mouse pointer") snipstuff.info.append("move the mouse pointer over the smiley to see it change color and click LMB to see a reaction.\n") snipstuff.info_show() base.disableMouse() #========================================================================= # Main """ To biefly sum up what happen below, we create 2 interacting objects: a FROM object, an invisible collision ray, and a INTO object, smiley. The FROM will generate collision events as soon as touch the INTO object moving the mouse pointer over it. """ #========================================================================= #** we settle the collision events as ususal base.cTrav=CollisionTraverser() collisionHandler = CollisionHandlerEvent() #** Differently from the previous steps, we change here a little bit our main FROM collider, # using now a strange kind of geometry: a ray - you may see it as a laser ray the constantly # shoot from the camera lenses toward the infinite. The following lines will settle up all of this. pickerNode=CollisionNode('mouseraycnode') pickerNP=base.camera.attachNewNode(pickerNode) pickerRay=CollisionRay() pickerNode.addSolid(pickerRay) base.cTrav.addCollider(pickerNP, collisionHandler) #** Here we make our same old smiley as we saw many times so far smileyModel = loader.loadModel('smiley') smileyModel.reparentTo(render)