import direct.directbase.DirectStart from direct.showbase.DirectObject import DirectObject from pandac.PandaModules import * from direct.interval.IntervalGlobal import *
In your code, you should disable the mouse and then place the camera at (1,-8,5) with a pitch of -25 degrees. Then define a class called World that derives from DirectObject (this allows World to receive events). In the constructor of the World class load and display two models: ./Models/factoryfloor.egg and ./Models/box.egg. Position the box at (1,0,2), scale it down to 30% and give it a heading of 45 degrees. Make sure you can see both the floor and the box.
<nodepath>.find
, and call it door. Create two LerpPosIntervals for the door, one to open it by moving it to location (-1,0,0) and one to close it by moving it to location (1,0,0). You can play with the duration but 2.0 seconds is a good place to start. Make the World accept a 'space' (spacebar key) event and call the associated event handler self.door_handler. In that handler, either start the opening interval for the door or the closing interval, depending on whether it's open or closed already (you'll need a new boolean member variable to keep track of that). Make sure you can now open and close the door with the spacebar.# HINT - Creating and attaching a CollisionSphere nodepath = object.attachNewNode(CollisionNode(<name>)) nodepath.node().addSolid(CollisionSphere(<x>,<y>,<z>,<radius>)) # You typically keep it at (0,0,0)
Similarly, attach a new CollisionNode to the door nodepath and call it door. This time however, add a CollisionPolygon to the node of the new nodepath. The verticies of this polygon are (-1,-1,0), (1,-1,0), (1,1,0) and (-1,1,0) in that order.
# HINT - The CollisionPolygon nodepath.node().addSolid(CollisionPolygon(Point3(x1,y1,z1),Point3(x2,y2,z2),Point3(x3,y3,z3),Point3(x4,y4,z4)))
You can call show() on your new collision node nodepaths to see your collision solids as semi-transparent objects when you view your scene. Verify that your solids are in the right place and then remove the calls to show().
collhandler = CollisionHandlerEvent() collhandler.addInPattern('%fn-into-%in') # The event name pattern: "<from name>-into-<into name>"
Then initialize the global collision traverser as follows:
base.cTrav = CollisionTraverser('world traverser')
and then add the box collision nodepath as a new collider and associate it with the collision handler you just created.
#HINT - Adding a new collider to a traverser base.cTrav.addCollider(<nodepath>, <handler>)
Make the world accept an event called “box-into-door” and call a method named self.collision where you can put things that should happen when a collision with this event name occurs. Notice that this method receives a parameter called collEntry which contains information about the collision event. The following is a simple collision event receiver:
def collision(self, collEntry): print collEntry
In addition to printing out some information like is done in this example, you should call the pause() method on the LerpPosInterval associated with the box object. This will stop the progress of the box movement. Verify that the box stops now when the door is closed but goes all the way down when the door is open.
thudsound = loader.loadSfx("thud.wav") closesound = loader.loadSfx("close.wav")
It should be pretty clear where to put the thudsound, but the closesound needs to be wrapped into a SoundInterval object and then sequenced with the closing door LerpPosInterval so that it happens at the end of the movement.
# HINT - Sequencing intervals newinterval = Sequence(<interval1>, <interval2>, ... )