Mouse picking

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Mouse picking

Post 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
Brinsky
Posts: 28
Joined: Sun May 08, 2005 3:05 pm
Location: Slovenia

Post by Brinsky »

Take a look at the irr collision example. Play with selectors. Hope you'll manage...
10 I lovez my computer
20 goto 10
AMAZINGAHMED
Posts: 8
Joined: Thu Apr 28, 2005 12:19 pm
Location: Egypt

Post 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.
THERE IS ALWAYS A FIRST TIME
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post 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
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post 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);


AMAZINGAHMED
Posts: 8
Joined: Thu Apr 28, 2005 12:19 pm
Location: Egypt

Post 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
THERE IS ALWAYS A FIRST TIME
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post 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?
AMAZINGAHMED
Posts: 8
Joined: Thu Apr 28, 2005 12:19 pm
Location: Egypt

Post 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.
THERE IS ALWAYS A FIRST TIME
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post 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".
It is like it is. And because it is like it is, things are like they are.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post 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
jox
Bug Slayer
Posts: 726
Joined: Thu Apr 22, 2004 6:55 pm
Location: Germany

Post by jox »

If you have switch statements in your receiver, check if all cases are properly breaked or returned.
It is like it is. And because it is like it is, things are like they are.
Cleves
Posts: 224
Joined: Mon Sep 08, 2003 6:40 pm

Post 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.
Post Reply