Page 1 of 1

Mouse picking

Posted: Sat May 14, 2005 5:28 pm
by Cleves
Hey,

I can't figure exactly how to do mouse picking. What I need is a simple drag n drop in a 3D scene using a mouse. For example, model standing on a plain terrain, using the mouse I would like to take the model using the mouse, and drag it to new location on the terrain, and then drop it there. Any ideas on the best way of achiving this?

Thanks

Posted: Sat May 14, 2005 8:24 pm
by Brinsky
Take a look at the irr collision example. Play with selectors. Hope you'll manage...

Posted: Sun May 15, 2005 8:29 am
by AMAZINGAHMED
if you want to select a complete node by the mouse then here's the solution:
first of all when you add the scene nodes, give each node an id (other than -1) and give your camera also an id.

use this code to detect collision:

void CheckCollision(int nodetID)
{
position2d<s32> cursor_position=device->getCursorControl()->getPosition();
selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromScreenCoordinatesBB(cursor_position,nodeID);
}

now you have selected a scene node which is stored into selectedSceneNode.

if you want to move the node, you may use the event receiver to detect that the mouse is down then change the position of the selected node according to the position of the mouse cursor.

Posted: Sun May 15, 2005 8:50 am
by Cleves
mmm, I've got it working yesterday but I've done it diffrently, and much longer, your solution is easier. Thanks a lot! :D

Posted: Sun May 15, 2005 10:03 am
by Cleves
mmmm, there is a strange problem with that code.

It seem he ignores the ID, and always puts 0, so all nodes are selected, no matter what's the ID.

Code: Select all


//That's my node, and I gave him ID 1
node = smgr->addAnimatedMeshSceneNode(mesh,NULL,1);



Posted: Sun May 15, 2005 10:45 am
by AMAZINGAHMED
note that for every selectable node, you must assign an id
i.e. id of health=1, id of ammo=2... etc.

or at least for every group, assign an id.

eg:
node_health = smgr->addAnimatedMeshSceneNode(mesh_health,NULL,0);
node_ammo = smgr->addAnimatedMeshSceneNode(mesh_ammo,NULL,1);
...etc

Posted: Sun May 15, 2005 12:39 pm
by Cleves
Yes, I forgot to ID one of my nodes, but now it works fine. Thanks.

Also, I have noticed some strange stuff happening when using this function. If I press my mouse twice, then the program quits, as if executing the function twice, gives an error. Also, after some testing, I have discovered that once in maybe 5 times, pressing the mouse will make all the objects in the scene disappear, regardless of their ID.

Maybe getSceneNodeFromScreenCoordinatesBB is bugged?

Posted: Sun May 15, 2005 1:08 pm
by AMAZINGAHMED
it works just fine with me and i get no errors.
maybe you're using an eventhandler that forces something wrong.
check your code.

Posted: Sun May 15, 2005 1:55 pm
by jox
Two points:

1. If a node has the ID "0" then it won't get picked at all. So give it only to nodes that you want to be ignored by the picking.

2. The camera has the default ID "-1". That means it matches all IDs. Due to something that I consider a bug, the camera gets picked sometimes/arbitrarily (I mentioned this before in some thread(s)). Sometimes just like in "once in maybe 5 times". So you better give your camera the ID "0".

Posted: Sun May 15, 2005 2:23 pm
by Cleves
Yes, that fixed it. thanks Jox.

I still get that wierd quit after 2 mouse presses, but I guess it's something with my event reciver.

Thanks a lot :D

Posted: Sun May 15, 2005 3:13 pm
by jox
If you have switch statements in your receiver, check if all cases are properly breaked or returned.

Posted: Sun May 15, 2005 3:34 pm
by Cleves
I've discovered something about that problem. It happens only after I try to pick something different then my first pick. if for example I pick model x, when i try to pick model x2, it quits.