I have got an object attached to the render 2D and I would like to determine if I’ve clicked on it - this works fine for 3D objects but for some reason doesn’t work for nodes attached to the render2D.
Here my code for attaching adding the node
NodePath render2D = GameWindow()->get_aspect_2d();
NodePath myNode = //load my model
myNode.reparent_to(render2D);
myNode.set_pos(0.0f, 0.0f, 0.0f);
and this is in my mouse event
NodePath render2D = GameWindow()->get_render_2d();
PT(CollisionRay) mPickerRay = new CollisionRay();
PT(CollisionNode) pickerNode = new CollisionNode("MousePicker");
pickerNode->add_solid(mPickerRay);
pickerNode->set_from_collide_mask(GeomNode::get_default_collide_mask());
NodePath mPickerNodePath = render2D.attach_new_node(pickerNode);
PT(CollisionHandlerQueue) mCollisionHQ2D = new CollisionHandlerQueue();
CollisionTraverser* mpCollisionTraverser2D = new CollisionTraverser("CollisionTraverser2D");
mpCollisionTraverser2D->add_collider(mPickerNodePath, mCollisionHQ2D);
mpCollisionTraverser2D->traverse(render2D);
if(mCollisionHQ2D->get_num_entries() > 0)
{
//do something
}
the collision handler queue seems to never have any entries, anyone knows what I am doing wrong?
The collision system isn’t really built for picking things from the 2-d scene graph, and it isn’t really necessary there anyway. The 2-d scene graph is easy; it’s just defined from -1 to 1 in both axes, so you can find what you’re looking for just by walking through a list.
But I think it probably ought to work anyway, to use the collision system. But you never set up the pickerRay from the mouse, it’s always just pointing straight down the Y axis. Normally you would call mPickerRay.setFromLens() to set up the ray to correspond to your mouse’s current position. In this case, the lens would have to be the lens from base.cam2d.node().getLens(), not the normal 3-d lens.