Which func to use?
Function which converts 2D to 3D
Function which converts 2D to 3D
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?
Which func to use?
Re: Function which converts 2D to 3D
What you are looking for is call "picking"; search for that.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?
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.
It mean mouse picking, i think so.
first, init some variable:
then, put this in your init function
in this code, we will create TriangleSelector from mesh file from Data/terrain.b3d.
and put this in your mainloop
now, when you click mouse in terrain scene node, it will set the 3d position to intersection.
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;
Code: Select all
selector = smgr->createTriangleSelector(smgr->getMesh("Data/terrain.b3d"),smgr->getSceneNodeFromId(100));
smgr->getSceneNodeFromId(100)->setTriangleSelector(selector);
selector->drop();
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);
Hey!, you should add that code into FAQ&& HOWTO&&etc, I think is very useful for newbies and not newbies.huydotnet wrote:It mean mouse picking, i think so.
first, init some variable:then, put this in your init functionCode: 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;in this code, we will create TriangleSelector from mesh file from Data/terrain.b3d.Code: Select all
selector = smgr->createTriangleSelector(smgr->getMesh("Data/terrain.b3d"),smgr->getSceneNodeFromId(100)); smgr->getSceneNodeFromId(100)->setTriangleSelector(selector); selector->drop();
and put this in your mainloopnow, when you click mouse in terrain scene node, it will set the 3d position to intersection.Code: Select all
pos = device->getCursorControl()->getPosition(); ray = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(pos,smgr->getActiveCamera()); smgr->getSceneCollisionManager()->getCollisionPoint(ray,selector,intersection,tri);
-
rogerborg
- Admin
- Posts: 3590
- Joined: Mon Oct 09, 2006 9:36 am
- Location: Scotland - gonnae no slag aff mah Engleesh
- Contact:
Are you being sarcastic? I honestly can't tell.
Note: this is demonstrated in example 07, cunningly titled "Collision".
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
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way