[SOLVED] Collision Traverser 2D


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.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");
NodePath mPickerNodePath = render2D.attach_new_node(pickerNode);
PT(CollisionHandlerQueue) mCollisionHQ2D = new CollisionHandlerQueue();
CollisionTraverser* mpCollisionTraverser2D = new CollisionTraverser("CollisionTraverser2D");
mpCollisionTraverser2D->add_collider(mPickerNodePath, mCollisionHQ2D);


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?

I’ve tried a couple of things but I’ve had no luck so far :frowning:

I’ve seen other people had similar problems in this forum but my code looks alright to me.

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.


actually I do set that like this

const LPoint2f& mpos = GetMouseWatcher()->get_mouse();
PT(Camera) pCamera = DCAST(Camera, mCamera.node());
mPickerRay->set_from_lens(pCamera, mpos.get_x(), mpos.get_y());

where “pCamera” is my custom camera. How would this translate into c++ ? base.cam2d.node()

Get the 2-d camera from the 2-d DisplayRegion:

Camera *camera2d = DCAST(Camera, window->get_display_region_2d()->get_camera().node());
Lens *lens2d = camera2d->get_lens();

Perfect as usual mate :slight_smile: It works.