collision rays don’t stop after the first collision
they continue and collide with all collision nodes behind the first
if you comment out lines 74-77, smiley’s collision node is gone
no collisions will occur with it
i.e. collision nodes are important
if you uncomment line 57 the collision ray with collide with all
visible objects and therefore it will collide with smiley even
if his collision node is commented out
i.e. collision nodes are not needed
import direct.directbase.DirectStart
from direct.showbase import DirectObject #necessary for event handling
from pandac.PandaModules import *
from direct.gui.OnscreenText import OnscreenText
import sys #to exit on escape......sys.exit
class World(DirectObject.DirectObject):
def __init__(self):
#This code puts the standard title and instruction text on screen
self.title = OnscreenText(
text="Panda3D: Tutorial - Mouse Picking",
style=1, fg=(1,1,1,1),
pos=(0.8,-0.95),
align=TextNode.ACenter, scale = .07)
self.escapeEvent = OnscreenText(
text="ESC: Quit",
style=1, fg=(1,1,1,1), pos=(-1.3, 0.95),
align=TextNode.ALeft, scale = .05)
self.mouse1Event = OnscreenText(
text="Left-click to pick an object",
style=1, fg=(1,1,1,1), pos=(-1.3, 0.90),
align=TextNode.ALeft, scale = .05)
self.onCollisionText = OnscreenText(
text="",
style=1, fg=(1,1,1,1), pos=(-1.3, 0.85),
align=TextNode.ALeft, scale = .05)
#Escape quits
self.accept('escape', sys.exit)
#initialize traverser
self.picker = CollisionTraverser()
#initialize handler
self.queue = CollisionHandlerQueue()
#loading
self.loadPicker()
self.loadObj1()
self.loadObj2()
self.loadObj3()
#waiting for the left mouse click
self.accept("mouse1", self.collisionCheck)
def loadPicker(self):
#attach a CollisionRay node to the camera
self.pickerNode = CollisionNode('mouseRay')
self.pickerNP = camera.attachNewNode(self.pickerNode)
# self.pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask())
self.pickerRay = CollisionRay()
self.pickerNode.addSolid(self.pickerRay)
self.pickerNP.show()
#add collision node (fromObject) to the traverser
self.picker.addCollider(self.pickerNP, self.queue)
self.picker.showCollisions(render)
def loadObj1(self):
#load a model. reparent it to the camera so we can move it.
smiley = loader.loadModel('smiley')
smiley.reparentTo(render)
smiley.setPos(0, 25.5,0.5)
#create a collision solid for this model
cNode = CollisionNode('smiley')
cNode.addSolid(CollisionSphere(0,0,0,1.1))
smileyC = smiley.attachNewNode(cNode)
smileyC.show()
def loadObj2(self):
#load a model
frowney = loader.loadModel('frowney')
frowney.reparentTo(render)
frowney.setPos(5, 25,0)
#create a collsion solid for this model
cNode = CollisionNode('frowney')
cNode.addSolid(CollisionSphere(0,0,0,1.1))
frowneyC = frowney.attachNewNode(cNode)
frowneyC.show()
def loadObj3(self):
#load a model
frowney2 = loader.loadModel('frowney')
frowney2.reparentTo(render)
frowney2.setPos(5, 35,0.2)
#create a collsion solid for this model
cNode = CollisionNode('frowney22')
cNode.addSolid(CollisionSphere(0,0,0,1.1))
frowneyC = frowney2.attachNewNode(cNode)
frowneyC.show()
def collisionCheck(self):
if base.mouseWatcherNode.hasMouse():
#get the mouse position
mpos = base.mouseWatcherNode.getMouse()
#aim the collision ray
self.pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())
#fire the ray and record, in the queue, any collisions
self.picker.traverse(render)
if self.queue.getNumEntries() > 0:
if self.queue.getNumEntries() == 1:
self.mytext = "A collision has occured with " \
+ str(self.queue.getEntry(0).getIntoNodePath())
else:
#organize the collisions from first hit to second...
self.queue.sortEntries()
#output the name of the node paths the ray collided with
self.mytext = "A collision occured first with " \
+ str(self.queue.getEntry(0).getIntoNodePath()) \
+ " and then with " + str(self.queue.getEntry(1).getIntoNodePath())
self.onCollisionText.setText(self.mytext)
w = World()
run()
#note: the collision ray doesn't stop after the first collision
# it continues and collides with all collision nodes behind the first
#note: if you comment out lines 74-77, smiley's collision node is gone
# no collisions will occur with it
#note: if you uncomment line 57 the collision ray with collide with all
# visible objects and therefore it will collide with smiley even
# if his collision node is commented out.i.e. collision nodes are
# not needed