Function which converts 2D to 3D

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
gogo
Posts: 65
Joined: Tue Apr 15, 2008 1:04 am

Function which converts 2D to 3D

Post by gogo »

Please image to let a character move to the place which is pointed by a mouse cursor in 3D RPG. So I am looking for a function of Irrlicht which converts 2D coordinate to 3D one.
Which func to use? :?:
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Re: Function which converts 2D to 3D

Post by Vsk »

gogo wrote:Please image to let a character move to the place which is pointed by a mouse cursor in 3D RPG. So I am looking for a function of Irrlicht which converts 2D coordinate to 3D one.
Which func to use? :?:
What you are looking for is call "picking"; search for that.
In a easy way (limited) picking is done by proyecting (x,y) coord from the screen + perpendicular vector (parallel to cam->getTarget vector).
The first object that this line intersect is the one which is picked.

You can easily use irrlicht collision detection for that, if there is not something already done by some user.
huydotnet
Posts: 81
Joined: Tue Sep 25, 2007 12:26 pm
Location: Danang, Vietnam

Post by huydotnet »

It mean mouse picking, i think so.
first, init some variable:

Code: Select all

irr::core::position2d<s32> mousePos; //This is mouse position (in 2D)
core::vector3df intersection; //Mouse position in 3d (after convert)
core::triangle3df tri; 
core::line3df ray;
scene::ITriangleSelector* selector;
then, put this in your init function

Code: Select all

selector = smgr->createTriangleSelector(smgr->getMesh("Data/terrain.b3d"),smgr->getSceneNodeFromId(100));
	smgr->getSceneNodeFromId(100)->setTriangleSelector(selector);
	
	selector->drop();
in this code, we will create TriangleSelector from mesh file from Data/terrain.b3d.
and put this in your mainloop

Code: Select all

pos = device->getCursorControl()->getPosition();
	ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(pos,smgr->getActiveCamera());
	smgr->getSceneCollisionManager()->getCollisionPoint(ray,selector,intersection,tri);
now, when you click mouse in terrain scene node, it will set the 3d position to intersection.
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post by Vsk »

huydotnet wrote:It mean mouse picking, i think so.
first, init some variable:

Code: Select all

irr::core::position2d<s32> mousePos; //This is mouse position (in 2D)
core::vector3df intersection; //Mouse position in 3d (after convert)
core::triangle3df tri; 
core::line3df ray;
scene::ITriangleSelector* selector;
then, put this in your init function

Code: Select all

selector = smgr->createTriangleSelector(smgr->getMesh("Data/terrain.b3d"),smgr->getSceneNodeFromId(100));
	smgr->getSceneNodeFromId(100)->setTriangleSelector(selector);
	
	selector->drop();
in this code, we will create TriangleSelector from mesh file from Data/terrain.b3d.
and put this in your mainloop

Code: Select all

pos = device->getCursorControl()->getPosition();
	ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(pos,smgr->getActiveCamera());
	smgr->getSceneCollisionManager()->getCollisionPoint(ray,selector,intersection,tri);
now, when you click mouse in terrain scene node, it will set the 3d position to intersection.
Hey!, you should add that code into FAQ&& HOWTO&&etc, I think is very useful for newbies and not newbies.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Are you being sarcastic? I honestly can't tell.

Note: this is demonstrated in example 07, cunningly titled "Collision".
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Vsk
Posts: 343
Joined: Thu Sep 27, 2007 4:43 pm

Post by Vsk »

rogerborg wrote:Are you being sarcastic? I honestly can't tell.

Note: this is demonstrated in example 07, cunningly titled "Collision".
haha, that's right.
I just forgot it (I have never used).
gogo
Posts: 65
Joined: Tue Apr 15, 2008 1:04 am

sample

Post by gogo »

:!:
Last edited by gogo on Sat Jul 05, 2008 11:11 am, edited 1 time in total.
Sigutis
Posts: 10
Joined: Thu May 01, 2008 8:51 am
Location: Lithuania

yes

Post by Sigutis »

gogo's file would make sense if it had any source included
Post Reply