Placing model based on mouse position

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
rpglover
Posts: 3
Joined: Thu Jun 19, 2008 11:48 pm

Placing model based on mouse position

Post by rpglover »

I have looked through the search, as well as the documentation and simply can't figure out a method to placing a model based on the the mouse coordinates. Can anyone explain a simple way of determining the mouse coordinates and placing a model based on those coordinates?
gogo
Posts: 65
Joined: Tue Apr 15, 2008 1:04 am

sample

Post by gogo »

Getting a mouse position on 2D

Code: Select all

position2d<s32> mouse_pos = Device->getCursorControl()->getPosition();
rpglover
Posts: 3
Joined: Thu Jun 19, 2008 11:48 pm

Post by rpglover »

Ok, so if thats a way as to obtaining the 2d coordinates how can I take the x and y position from their? This is how I assume I would place the model so far:

Code: Select all

    
position2d<s32> mouse_pos = device->getCursorControl()->getPosition();
Model->setPosition(vector3df(?, ?, ?));
Or is their another way to determine the coordinates? What I'm trying to achieve is moving the model through the mouse coordinates while the left mouse button is down, once it is lifted the model will be placed at that position.
renegadeandy
Posts: 122
Joined: Sun May 25, 2008 11:14 pm
Location: Scotland
Contact:

Post by renegadeandy »

thats what I have been doing for a while now , look for my threads in this Beginners help forum - most are relevant!

you want to basically :

sent out a ray from your mouses position
set the objects position on the intersection of the ray and terrain model

Check out the collision detection example to find out how to do the collision & ray stuff.
Jgoldnight
Posts: 31
Joined: Thu Jun 07, 2007 6:23 pm
Location: New York
Contact:

Post by Jgoldnight »

If you set the object's position as the intersection between the ray and terrain, all your objects will be placed along the terrain, which is restrictive.

My current algorithm includes setting the selected object as a child of the camera (when the left mouse is held down), thus, allowing the selected node to move along with the mouse (accounting for both translation and rotation). The problem that I am finding is that when you release the mouse button, the obvious thing to simulate "releasing" the node, would be to remove the parent-child relationship. Unfortunately, Irrlicht's implementation of setParent() involves removing the calling node from the scene completely. I am still thinking of how to bypass this handicap (besides modifying the engine).

In short, this method will give the effect of moving a node around the world, but the releasing code needs to be reworked.

Hope it helps.
Computer scientist by day...
http://www.mrjoelkemp.com
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Jgoldnight wrote:If you set the object's position as the intersection between the ray and terrain, all your objects will be placed along the terrain, which is restrictive.
This is quite possibly what he wants.
Unfortunately, Irrlicht's implementation of setParent() involves removing the calling node from the scene completely. I am still thinking of how to bypass this handicap (besides modifying the engine).
This will happen only if you call node->setParent(0) or if the parent pointer you pass to setParent() is a pointer to a node that is not in the scene graph.

One option you have is to reparent the scene node to the root scene node. Unless some other parent node is specified, a scene node is a child of the scene graphs root scene node. You can get that scene node from the scene managers getRootSceneNode() method. So you could write node->setParent (smgr->getRootSceneNode()) to make node a child of the root scene node. The other solution, which could be considered to be more robust, would be to cache a pointer to the node parent before you call setParent() the first time. Then, when you are done moving the node you would set the parent back to the previous parent.

One problem that you will run into here is that when you reparent a node, it changes the nodes absolute transformation. So you need to modify its relative transformation to make the node appear in the same place after reparenting it.

Travis
Jgoldnight
Posts: 31
Joined: Thu Jun 07, 2007 6:23 pm
Location: New York
Contact:

Post by Jgoldnight »

Thanks Vitek!
Computer scientist by day...
http://www.mrjoelkemp.com
Post Reply