how to place an object where the mouse points

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.
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

how to place an object where the mouse points

Post by belva1234 »

Hy I tried for several hours yesterday to try using this code but nothing I 've finished idea can you help me?? this is the code

Code: Select all

void SchootBlocks(){
    scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
 
        //da modificare la texture blocco in base all blocco che si e selezionato
     IAnimatedMesh *blocco=smgr->getMesh("../../media/blocco.3ds");
     IAnimatedMeshSceneNode *blocconode=smgr->addAnimatedMeshSceneNode(blocco);
    blocconode->setMaterialFlag(video::EMF_LIGHTING,false);
                core::line3d<f32> ray;
                ray.start = cam->getAbsolutePosition();
                ray.end = ray.start + (cam->getTarget() - ray.start).normalize()*1000000.0f;
                core::vector3df intersection;
                core::triangle3df hitTriangle;
                scene::ISceneNode* selectedSceneNode =collMan->getSceneNodeAndCollisionPointFromRay(ray,intersection,hitTriangle);
                if (selectedSceneNode)
            blocconode->setPosition(intersection);
            else
            printf("NON FUNZIONA MERDA\n");
}
Non funziona merda...
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: how to place an object where the mouse points

Post by Mel »

I use a system in which i place an object with regard the plane it is contained into. Basically:

-find the horizontal plane where the object lies.This is easy because you have a point, that is the object location, and the normal is the vertical vector, (0,1,0)
-find the ray where the mouse is pointing at. The collision manager is able to find this ray quickly
-place your object in the intersection between the ray and the plane. Normally, it works quite well and fast enough so it can be done in real time.
-If you want to place your object into another plane, just move it vertically.

In a nutshell, this is pretty much it.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

I'm Italian and I understand in part, you can modify the code please
Non funziona merda...
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: how to place an object where the mouse points

Post by Mel »

"Non funzona, Merda" LOL (i'm spanish, i got this part XDDDDD)

It isn't very hard to implement :)

Say you have a scene node, you have picked it up somehow... When i say mousePosition, i mean a point2D with the position of the mouse...

Code: Select all

 
scene::ISceneNode* node;
 
node = collMan->getSceneNodeFromScreenCoordinatesBB (mousePosition);
if(node)
{
//-find the horizontal plane where the object lies.This is easy because you have a point, that is the object location, and the normal is the vertical vector, (0,1,0)
core::plane3d plane;
plane.setPlane(node->getLocation(),core::vector3df(0,1,0));
 
//-find the ray where the mouse is pointing at. The collision manager is able to find this ray quickly
core::line3df ray = collMan->getRayFromScreenCoordinates(mousePosition,sceneManager->getActiveCamera());
 
//-place your object in the intersection between the ray and the plane.
core::vector3df point;
plane.getIntersectionWithLine(ray.start,ray.end,point);
node->setPosition(point);
}
 
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

ok but I do not understand 2 things: 1) node in my code corresponds to the node of the block?? 2) MousePosition ... not declared Thanks very much for the help
Non funziona merda...
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

Anyone can help me??????????
Non funziona merda...
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: how to place an object where the mouse points

Post by CuteAlien »

Sorry, we can't really help you unless you know how to code a little bit more. I just have no idea how we can't help you using a c++ 3d-engine unless you can at least write enough c++ to understand stuff like posted above. You can't do a game and expect other people to write the functions for you, that just doesn't work. Crossposting the same questions over and over in the forum also doesn't help unless you are already knowing enough to actually understand the answers!

Please start with simpler programs than a 3D game, to get really familiar with c++ first. Maybe you could even start by working your way through the Irrlicht examples, but I would rather recommend to start with simpler code. I started myself doing text-only applications for a while before I switched to 2D and then later switched to 3D programming.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

I know enough about the c + + but also tell me if I knew the C + + well but did not know the functions of irrlicht. How would I do? Then if I try to learn but nobody can help me...
Non funziona merda...
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: how to place an object where the mouse points

Post by CuteAlien »

The problem is we are trying to help you... Mel gave you the core of one solution how to solve a collision with floor. I tried to tell you about how to find the troubles in your other code. But you expect finished solutions and have still trouble with function parameters and undeclared variables. That is a sign that you should work on simpler problems for now. This stuff gets easier after you have coded a few simpler projects. But learning this at the same time while struggling with learning 3d programming - you're making your life hard - and annoy other people with the wrong questions because you skipped too many steps in your learning. You don't see we already told you the hard parts because you are still struggling with the easy parts.

It is typical when you're new to programming to expect seeing complete working functions. But once you start really programming it is up to you to figure out how to write such functions. And yes, this takes long and we try to help you - but we won't _do_ it for you - because figuring out such functions _is_ what a programmer does. You don't ask in the forum after you're stuck a little. You figure it out yourself and ask in a forum once you really have tried everything. You don't post the same code over and over and expect people to write it perfect for you. You write it yourself and add debugging information and use a debugger and read documentation etc. and you spend days and weeks on it. And yes, you have to learn how to add debug-information. And you have to learn how to use a debugger. And you have to learn how to read API documentation. Those are the easy parts with which you have be comfortable to be able to work on harder problems. That is how you learn. And _then_ you are able to ask specific question. Because you can show screenshots with your debug-output. You can tell exactly which parts are causing you trouble and you can tell what the debugger said. And maybe you will even run at some point in a function that does not behave as the documentation said and you can show it with a test-case you wrote. And you don't have to ask for a function which does everything for you anymore - you won't need it because after a few hints you will figure out the rest yourself.

I don't want to de-motivate you. I just want you learning stuff in a order that isn't frustrating to everyone. If you have the feeling you can't continue this despite all the hints you got already then please start with simpler problems. You will learn faster and can get back to this problem once you feel confident you can solve this. No-one (or nearly no-one) just starts out as 3d programmer.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

Ok I understand and I've almost solved my problem just a question even then I don' t speak more xD .how found I the position of the mouse?
Non funziona merda...
CuteAlien
Admin
Posts: 9679
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: how to place an object where the mouse points

Post by CuteAlien »

belva1234 wrote:how found I the position of the mouse?
Do you mean to ask us how to find it or do you want us to guess how you found it? :-)

IrrlichtDevice::getCursorControl for getting cursor information. ICursorControl then has functions for 2d mouse-positions: http://irrlicht.sourceforge.net/docu/cl ... ntrol.html

Also from ISceneManager with getSceneCollisionManager you get http://irrlicht.sourceforge.net/docu/cl ... nager.html
There you have functions to get the ray of positions in the 3D world for a 2D mouse position and a given camera position.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

ok maybe I understood. using the solution of mel I changed the code:

Code: Select all

 scene::ITriangleSelector* selector;
    scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
    irr::core::position2d<s32>  mousePosition= device->getCursorControl()->getPosition();
        //da modificare la texture blocco in base all blocco che si e selezionato
     IAnimatedMesh *blocco=smgr->getMesh("../../media/blocco.3ds");
     IAnimatedMeshSceneNode *blocconode=smgr->addAnimatedMeshSceneNode(blocco,smgr->getRootSceneNode());
    blocconode->setMaterialFlag(video::EMF_LIGHTING,false);
    scene::ISceneNode* node;
    node = collMan->getSceneNodeFromScreenCoordinatesBB (mousePosition);
            if(node)
            {
            core::plane3d plane;
            plane.setPlane(node->getLocation(),core::vector3df(0,1,0));
            core::line3df ray = collMan->getRayFromScreenCoordinates(mousePosition,smgr->getActiveCamera());
            core::vector3df point;
            plane.getIntersectionWithLine(ray.start,ray.end,point);
            blocconode->setPosition(point);
            }
but the error log show some errors: Missing teplate aguments befor plane
Non funziona merda...
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Re: how to place an object where the mouse points

Post by nespa »

1. create a triangle selector and set this selector to your plane mesh node;
2. the intersection will be with the horizontal plane, not with your node;
3. the outnode is the collided node, (not your mesh node to be moved);

example code:

Code: Select all

DLL_EXPORT void* DLL_APIENTRY nGetNodeAndCollisionPointFromCamera(
                                    ICameraSceneNode* camera,
                                                                        //ISceneNode *planenode,
                                                                        ITriangleSelector* selector,
                                    float* xcoll,
                                                                        float* ycoll,
                                                                        float* zcoll)
                                 
        {
 
        //planenode->setTriangleSelector(selector);
        //selector->drop();
        scene::ISceneCollisionManager* collMan = SceneManager->getSceneCollisionManager();
        core::line3d<f32> ray;
        ray.start = camera->getPosition();
        ray.end = ray.start + (camera->getTarget() - ray.start).normalize() * 10000.0f;
        core::vector3df intersection;
        core::triangle3df hitTriangle;
        scene::ISceneNode * outNode =
                        collMan->getSceneNodeAndCollisionPointFromRay(
                                        ray,
                                        intersection, // This will be the position of the collision
                                        hitTriangle, // This will be the triangle hit in the collision
                                        1<<0, // This ensures that only nodes that we have
                                                        // set up to be pickable are considered
                                        0); // Check the entire scene (this is actually the implicit default)
 
        *xcoll=intersection.X;
        *ycoll=intersection.Y;
        *zcoll=intersection.Z;
         
    return (void*) outNode;
        }
 
then : blocconode->setPosition(intersection); to move your mesh at intersection point with planenode;
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

I replaced the code and adapting it to my , is correct right?

Code: Select all

void SchootBlocks(){
    scene::ITriangleSelector* selector;
    scene::ISceneCollisionManager* collMan = smgr->getSceneCollisionManager();
    irr::core::position2d<s32>  mousePosition= device->getCursorControl()->getPosition();
        //da modificare la texture blocco in base all blocco che si e selezionato
     IAnimatedMesh *blocco=smgr->getMesh("../../media/blocco.3ds");
     IAnimatedMeshSceneNode *blocconode=smgr->addAnimatedMeshSceneNode(blocco,smgr->getRootSceneNode());
    blocconode->setMaterialFlag(video::EMF_LIGHTING,false);
    ISceneNode *planenode;
    planenode->setTriangleSelector(selector);
    selector->drop();
        core::line3d<f32> ray;
        ray.start = cam->getPosition();
        ray.end = ray.start + (cam->getTarget() - ray.start).normalize() * 10000.0f;
        core::vector3df intersection;
        core::triangle3df hitTriangle;
        scene::ISceneNode * outNode =collMan->getSceneNodeAndCollisionPointFromRay(
                                        ray,
                                        intersection, // This will be the position of the collision
                                        hitTriangle, // This will be the triangle hit in the collision
                                        1<<0, // This ensures that only nodes that we have
                                                        // set up to be pickable are considered
                                        0); // Check the entire scene (this is actually the implicit default)
blocconode->setPosition(intersection);
}
Non funziona merda...
belva1234
Posts: 44
Joined: Sat Apr 07, 2012 2:00 pm

Re: how to place an object where the mouse points

Post by belva1234 »

when I run the program there is an error Run time: the variable 'selector'is being used without being initialized
Non funziona merda...
Post Reply