Thanks for your replies. They helped me figure it out. The collision node is created in the missile constructor.
After copying and pasting code into a response, I actually figured out how to do what I needed to do. But now I have another issue related to it. Chalk this up to me not fully understanding the scene graph and/or not understanding how to traverse it…
Here is the code I fixed where the missile is instantiated:
self.missile = Missile(shipVector, hudVector, taskname)
# shipVector = player position
# hudVector = hidden object in front of and parented to the player' ship
# taskname = random number I use internally to debug and learn how everything works
self.shooting = 0 #this is a variable that increments later to control how often a player can fire
self.name = self.missile.getnodename() # debug/learning
self.nodename = self.missile.getnodepath() # debug/learning
print self.nodename debug/learning
self.traverser.addCollider(self.missile.collisionnode, self.handler) # this line fixed the code
#'collisionnode' is the name of the collision node created in the missile's constructor - I didn't realize it was a property of each instance
Here is the missile class:
class Missile(object):
maxRange = 50
def __init__(self, shipVector, hudVector, taskname): #explained in instancing code
self.directionVector = hudVector - shipVector
self.directionVector.normalize()
self.fireVector = (self.directionVector * self.maxRange) + hudVector
self.missileModel = loader.loadModel('models/jbickelmissile.egg')
self.missile = render.attachNewNode("missile" + taskname)
self.missileModel.instanceTo(self.missile)
self.collisionnode = self.missile.attachNewNode(CollisionNode('collisionnode'))
self.collisionnode.node().addSolid(CollisionSphere(0,0,0,.03))
self.missileModel.setScale(.02, .02, .02)
self.missile.reparentTo(render)
self.ttl = 200
taskMgr.add(self.rotateMissile, 'rotate' + taskname)
taskMgr.add(self.moveMissile, 'move' + taskname)
self.taskname = taskname
self.interval = LerpPosInterval(self.missile, 3, self.fireVector, hudVector, fluid = 1)
self.interval.start()
def rotateMissile(self, task): # the missile is more like a star Trek photon torpedo and looks better like this
self.h = random.randint(0, 359)
self.p = random.randint(0, 359)
self.r = random.randint(0, 359)
self.missileModel.setHpr(self.h, self.p, self.r)
return Task.cont
def moveMissile(self, task): #ttl = time to live - makes missile self destruct after so long a time
self.ttl -= 1
if self.ttl < 1:
taskMgr.remove('rotate' + self.taskname)
taskMgr.remove('move' + self.taskname)
self.explode(self.missile)
return Task.cont
def explode(self, missile):
missile.removeNode()
def getnodename(self): #debug/learning
return self.missile.getName()
def getnodepath(self): #debug/learning
return self.missile.node()
The new issue has to do with how the professor is having us deal with collisions. Here is how we have them set up:
def setupCollision(self):
self.traverser = CollisionTraverser()
self.pusher = CollisionHandlerPusher()
self.handler = CollisionHandlerEvent()
self.handler.addInPattern('into')
self.accept('into', self.handleInto)
base.cTrav = self.traverser
self.traverser.traverse(render)
#pass collisionNodePath and ModelNodePath to the Handler
self.pusher.addCollider(self.playershipCnode, self.playership)
self.traverser.addCollider(self.playershipCnode, self.pusher)
def handleInto(self, entry):
fromNode = entry.getFromNode().getName() #debug/learning
fromNodePath = entry.getIntoNodePath() #debug/learning
intoNode = entry.getIntoNode().getName() #debug/learning
print entry
print intoNode
print fromNode
fromNodePath.explode()
print str(fromNodePath)
While running the above I shoot a drone ship, the output is…
Known pipe types:
wglGraphicsPipe
(all display modules loaded.)
CollisionEntry:
from render/missile238712/collisionnode
into render/jbickeldrone.egg/droneCnode []
at -6.70587 -1.30329 0
normal 2.99467 0.178824 0
interior -6.73434 -1.30499 0 (depth 0.0285231)
respect_prev_transform = 0
droneCnode
collisionnode
render/jbickeldrone.egg/droneCnode
Concerning the above, is “render/modelname/collision node name” the node path?
When the missile hits something, I want it to call a function that makes it explode and etc. But the collision info only deals with the collision nodes. How do I find the node that the collision node is attached to so I can call the “explode” function? Is that the right way to approach this? I checked into a couple of methods that sounded like they would get the parent of the collision node, but I have had no success. It seems like there has to be a way to go from node path to object and vice versa, but I haven’t figured it out yet…