Mouse picking
Mouse picking
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
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
-
- Posts: 8
- Joined: Thu Apr 28, 2005 12:19 pm
- Location: Egypt
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.
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
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.
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);
-
- Posts: 8
- Joined: Thu Apr 28, 2005 12:19 pm
- Location: Egypt
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
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
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?
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?
-
- Posts: 8
- Joined: Thu Apr 28, 2005 12:19 pm
- Location: Egypt
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".
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.