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.
# HINT - Creating and attaching a CollisionSphere nodepath = object.attachNewNode(CollisionNode('object')) nodepath.node().addSolid(CollisionSphere(0,0,0,radius))
Similarly, attach a new CollisionNode to the door pathnode and call it door. This time however, add a CollisionPolygon to the node of the new pathnode. 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(x,y,z),Point3(x,y,z),Point3(x,y,z),Point3(x,y,z)))
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')
Initialize the global collision traverser as follows:
base.cTrav = CollisionTraverser('world traverser')
and then add the nodepath to your box collision node as a new collider and associate it with the 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 paramater 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, ... )