Mouse click on object

hello, i have an issue, i want to detect when i click on my object,
i read this doc:
https://docs.panda3d.org/1.10/python/programming/collision-detection/clicking-on-3d-objects

but some part is unclear for me, maybe i not correct load the collision.

from direct.showbase.ShowBase import ShowBase
from panda3d.core import CollisionTraverser, CollisionNode, CollisionHandlerQueue
from panda3d.core import CollisionRay, GeomNode, CollisionSphere
from panda3d.core import Point3

class ClickableObjectDemo(ShowBase):
    def __init__(self):
        super().__init__()
        self.cube = self.loader.loadModel("models/box")
        self.cube.reparentTo(self.render)
        self.cube.setPos(0, 10, 0)
        self.setup_collision()
        self.accept("mouse1", self.on_click)

    def setup_collision(self):
        self.cTrav = CollisionTraverser()
        self.collisionHandler = CollisionHandlerQueue()
        cube_collision_node = CollisionNode('cube_collision')
        cube_collision_node.addSolid(CollisionSphere(0, 0, 0, 1))
        cube_collision_np = self.cube.attachNewNode(cube_collision_node)
        cube_collision_np.setCollideMask(1)
        self.picker_ray = CollisionRay()
        picker_node = CollisionNode('mouse_ray')
        picker_node.addSolid(self.picker_ray)
        self.picker_np = self.camera.attachNewNode(picker_node)
        self.picker_np.setCollideMask(1)
        self.cTrav.addCollider(self.picker_np, self.collisionHandler)

    def on_click(self):
        if not self.mouseWatcherNode.hasMouse():
            return
        mouse_pos = self.mouseWatcherNode.getMouse()
        self.picker_ray.setFromLens(self.camNode, mouse_pos.getX(), mouse_pos.getY())

        self.cTrav.traverse(self.render)
        print("click")
        if self.collisionHandler.getNumEntries() > 0:
            self.collisionHandler.sortEntries()
            entry = self.collisionHandler.getEntry(0)  # L'entrée la plus proche
            clicked_node = entry.getIntoNodePath()
            if clicked_node.hasTag('cube_collision'):
                print("YESSSS")

app = ClickableObjectDemo()
app.run()

i capture the right click, but i not detect the click on the box.
du you have an idea ?

You are filtering by the tag that you forgot to set.

cube_collision_node.setTag('cube_collision', '')

You can filter by name, it will work.

if clicked_node.name == "cube_collision":

I also note that you appear to be using only the “setCollideMask” method of NodePath. But that method only sets the into collide-mask, not the from collide-mask.

As a result, I’m not sure that your collisions will occur at all.

(And you might get warnings about attempting to collide “into” a CollisionRay, which isn’t supported.)

I’d suggest using the “setFromCollideMask” of the PandaNode class to set the “from” collide-mask of your ray, and to perhaps set the “into” collide-mask of your ray to “0” (so that it’s only a “from”-collider, never an “into”-collider).

Something like so:

picker_node CollisionNode('mouse_ray')
picker_node.setFromCollideMask(1)
# Noting above that "CollisionNode" is a sub-class of "PandaNode", and thus has
#  all methods available to "PandaNode".
self.picker_np = self.camera.attachNewNode(picker_node)
self.picker_np.setCollideMask(0)
# Or: picker_node.setIntoCollideMask(0)

thx it’s work !