Clicking on 3D Objects
The simplest way to click on 3D objects in Panda3D is to use very simplistic
collision detection coupled with event processing. First, after a
CollisionTraverser and a
CollisionHandler have been set up,
CollisionRay node to the camera. This node will have its
“from” collision mask set to
order to be as general as possible.
pickerNode = CollisionNode('mouseRay') pickerNP = camera.attachNewNode(pickerNode) pickerNode.setFromCollideMask(GeomNode.getDefaultCollideMask()) pickerRay = CollisionRay() pickerNode.addSolid(pickerRay) myTraverser.addCollider(pickerNP, myHandler)
For any object that you want to be pickable you should add a flag to it. The
easiest way is to use the
object1.setTag('myObjectTag', '1') object2.setTag('myObjectTag', '2')
The above example sets the tag
'myObjectTag' on two objects in your graph
that you want to designate as pickable. We will check for the presence of this
tag after we get the response back from the collision system.
Because Actors use a different
set-up, the collision system will return the geometry but not the NodePath.
instead to return the node path of an Actor.
Now assume that the function
myFunction() is set up to be called for the
'mouse1' event. In
myFunction() is where you call
pickerRay.setFromLens(origin, destX, destY).
This makes the ray’s origin
origin and the ray’s vector the direction from
origin to the point (
def myFunction(): # First we check that the mouse is not outside the screen. if base.mouseWatcherNode.hasMouse(): # This gives up the screen coordinates of the mouse. mpos = base.mouseWatcherNode.getMouse() # This makes the ray's origin the camera and makes the ray point # to the screen coordinates of the mouse. pickerRay.setFromLens(base.camNode, mpos.x, mpos.y)
After this, you now call the traverser like any other collision, get the closest object and “pick” it.
def myFunction(): mpos = base.mouseWatcherNode.getMouse() pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY()) myTraverser.traverse(render) # Assume for simplicity's sake that myHandler is a CollisionHandlerQueue. if myHandler.getNumEntries() > 0: # This is so we get the closest object myHandler.sortEntries() pickedObj = myHandler.getEntry(0).getIntoNodePath()
The node returned by the collision system may not be the object itself, but
might be just a part of the object. In particular, it will be one of the
GeomNodes that make up the object. (The
GeomNode class contains
the visible geometry primitives that are used to define renderable objects in
Panda3D.) Since your object might consist of more than one
what you probably would prefer to get is the
NodePath that represents
the parent of all of these GeomNodes that is, the
NodePath that you
'myObjectTag' tag on above. You can use
NodePath.findNetTag() to return the parent
contains a specified tag. (There are also other, similar methods on
NodePath that can be used to query the tag specified on a parent
node, such as
For simplicity, we shall restrict this example to
Now you can edit
myFunction() to look like this:
def myFunction(): mpos = base.mouseWatcherNode.getMouse() pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY()) myTraverser.traverse(render) # Assume for simplicity's sake that myHandler is a CollisionHandlerQueue. if myHandler.getNumEntries() > 0: # This is so we get the closest object. myHandler.sortEntries() pickedObj = myHandler.getEntry(0).getIntoNodePath() pickedObj = pickedObj.findNetTag('myObjectTag') if not pickedObj.isEmpty(): handlePickedObject(pickedObj)