re. Finding the mouse coordinates in the 3d world

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

re. Finding the mouse coordinates in the 3d world

Post by DGENXP »

Hi

If this is possible could you help me please

If i wanted to select an object that is in the 3d world how would i go about finding the coordinates of the mouse in the 3d world so i could select the object that is closest to the mouse

Cheers
DGEN XP
Ced666
Posts: 86
Joined: Fri Jan 13, 2006 10:29 am
Location: Belgium

Post by Ced666 »

The mouse coordinates cannot be converted simply to a 3D position (because there is one dimension more). Instead, the mouse position can give you a ray that start from the camera position and goes 'inside' your screen. With that ray, you'll have to check for the collisions with other object.

Take a look at the function getRayFromScreenCoordinates from the ISceneCollisionManager. You can then use getCollisionPoint to check the collision with a triangle selector (constructed from a scene node).
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Post by DGENXP »

Hi

Thank you for your quick reply

I have read the api and i am assuming that where pos is on the command i will need to place the x and y co-ordinates on the mouse position.

getRayFromScreenCoordinates ( core::position2d< s32 > pos, IsceneNode * camera = 0)

would i need to place the command in the while loop and now would i go by using this in my app

any further questions please post here.
Ced666
Posts: 86
Joined: Fri Jan 13, 2006 10:29 am
Location: Belgium

Post by Ced666 »

What do you want to do exactly ? Select a node when you click on it probably (that seems the most logical to me) ?
Then, why don't you simply check when you click with the mouse ? Don't need to calculate this all the time in a loop.
where pos is on the command i will need to place the x and y co-ordinates on the mouse position.
I don't get you. Could you be more specific ?
Rambus
Posts: 96
Joined: Thu Nov 24, 2005 4:56 pm
Location: Canada
Contact:

Post by Rambus »

To simply select an object in a 3d world, you dont need to know a virtual 3d cord for your mouse, you just need to use a pick command on the 2d cords where your mouse is at. (If thats what you want to do, read on)
ISceneNode* selectedSceneNode = smgr->getSceneCollisionManager()->getSceneNodeFromScreenCoordinatesBB(curs->getPosition());

were curs is your irrlicht cursor and smgr is your scene manager.
and thats all you have to do, to pick individual nodes, no fancy code
”Ì0D
Hope that helps!
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Post by DGENXP »

Hi Thank you again for your reply.

ok my software creates an array that contains all of the floor tiles that are in my scene i will paste the code following to this. i would like to be able to select nodes that are in the array by clicking on them in order to do this i thought that you may need to also store all of the coordinates of each tile in an array as well

the below equation is not exactly what i am going to do it is just a brief description of what i want it to do
MPOS.X < Tile(i).X + 512 ( X = MPOS.X - Tile(i).X - X +Tile(i).x )

Below is the code that defines and places the tiles

Code: Select all

for (int Boxes = -Ammountofblocks*Ammountofblocks*1024*3; Boxes < 1024*Ammountofblocks*Ammountofblocks; Boxes+=1024) 
{ 
   nodes.push_back(smgr->addAnimatedMeshSceneNode(mesh)); 
} 

// Initialise the created nodes 

for (size_t i = 0; i < nodes.size(); ++i) 

{ 
 if (AblocksLine >= Ammountofblocks*1024) {Ablocksdown +=  1024;AblocksLine =-Ammountofblocks*1024;}
      nodes[i]->setMaterialFlag(EMF_LIGHTING, true); 
      nodes[i]->setMaterialTexture( 0, driver->getTexture("Objects/Floor10.jpg") );

      nodes[i]->setPosition(vector3df(AblocksLine,0,Ablocksdown)); 
  nodes[i]->setMaterialFlag(EMF_FOG_ENABLE,true);
AblocksLine +=1024;
//nodes[i]->setDebugDataVisible(true);
Tempy[i]=Ablocksdown ;
Tempx[i]=AblocksLine-1024;

} 
in the previous post by rambus how would i go about using it for my code

please ask if you have any more questions

cheers

DGENXP
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

hi,

i want to get a the point on a node from my mouse cursor position. i did like explained above and wrote this code:

Code: Select all

line3d<f32> line = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(device->getCursorControl->getPosition(), pCamera);
but it says
206 C:\Dokumente und Einstellungen\Kai\Eigene Dateien\age of war\Editor\main.cpp invalid use of member (did you forget the `&' ?)
206 C:\Dokumente und Einstellungen\Kai\Eigene Dateien\age of war\Editor\main.cpp base operand of `->' is not a pointer
but isnt everything in my code a pointer?

greets,
halan
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

getCursorControl is a function. You need to put parens after it so the compiler knows you are trying to call the function...

Code: Select all

device->getCursorControl()->getPosition();
Halan
Posts: 447
Joined: Tue Oct 04, 2005 8:17 pm
Location: Germany, Freak City
Contact:

Post by Halan »

yeah thanks

im stupid ;)

greets,
halan
Post Reply