Skip to main content
GameDev.net gamedev.net
🔒 Locked

Moving Object in 3D with mouse

Started by BlackDuck Aug 9, 2009 at 6:58 AM 8 replies 11.4k views
Original Post
BlackDuck
BlackDuck
Hi, I wish to take this program to the next stage and be able to click on the manipulators and move my object in that axis. Where would be the best resource to find out how I may do this. I noticed that the mouse coordinates are in screen coordinates (y reversed) and figured that that won't work with the global world coordinates. Latest screenshot Hosted by imgur.com
SiCrane
SiCrane
You can convert from screen coordinates to object coordinates with gluUnproject(). For more information you may want to put "object picking opengl" or "object selection opengl" into your favorite search engine and look at chapters 13 and 14 in the red book.
Pto
Pto
As SiCrane said gluUnproject() is probably what your looking for right now. Its pretty easy to get your head around, and google will show you the rest.

If you'd like to take it even further then I highly suggest this tutorial:

http://www.3dkingdoms.com/selection.html

Worked wonders for me!
BlackDuck
BlackDuck
Hi, thank you for the two replies. So that I am clear. I have the colour picking technique in place on my objects and when I click an object, the manipulators appear at the centre of the object as shown in the picture. Each manipulator guide has also been included in the colour picking.

I will do some research into gluUnproject().

Thanks
BlackDuck
BlackDuck
Now that I think about it, was that clear. That I do have picking. I am seeking a good place to start to learn once I click on the y axis (yellow arrow) and drag my mouse upwards. I wish for the cube to move up.

My issue is the mouse works in screen coordinates and I wish to understand how to put this in global world coordinates.

Do I still research into gluUnproject().
steven katic
steven katic
partly, maybe....


http://www.codeproject.com/KB/openGL/TranslationController.aspx

might help.
sirGustav
sirGustav
1. check if the cursor is on any of the axis
2. determine the direction of that axis in world-space
3. store the original position of the object
4. store the world-space cursor pos on the axis
5. for each mouse-move get the world-space cursor position on that axis, and translate the object

That's how I would do it. Personally I would transform the axis into screen-space(gluProject) instead of transforming the cursor to world-space(gluUnproject) since it is easier to ignore the depth-value, and you probably want to do that(though you probably want to check the sign) :)
Waterwalker
Waterwalker
As I understand you alreahy have means to click on an axis using the mouse. So I understand you can unproject your 2d mouse position of the initial click on the axis to 3d and figure out which axis is concerned. Lets call the initial mouse position 2d_a and 3d_a.

Not just set a flag in your input progressing that the mouse is dragging this particular axis. Upon each mouse move calculate the 2D delta, the distance the mouse moved from 2d_a to the current mouse position we now name 2d_b. So 2d_delta = 2d_b - 2d_a.

Now you just want to have this 2d_delta as 3d delta vector. You can do so by unprojecting 2d_b to a 3d_b using the same depth value where 2d_a projected onto the axis as 3d_b. Now you have 3d_a and 3d_b and can calculate delta_3d = 3d_b - 3d_a.

Finally, this delta_3d is the vector you need to translate your object to make it follow the mouse position. There might be smarter math ways to do this but this general appraoch works with each and every projection type supported by you unproject function.
------------------------------------I always enjoy being rated up by you ...
NumberXaero
NumberXaero
I just did something similar to this when someone informed me that for what Im doing was more complicated than it needed to be for what I was trying to accomplish. I was going to implement selection and dragging of some manipulator widget when all I really wanted was to move the objects into the correct place. So instead of worrying about selecting the manipulator itself (assuming you have a list of, or atleast one object already selected) I simply interpreted the mouse movement and a key/mouse button combination to mean movement along an axis, much like how the Unreal Editor works

ctrl + left mouse + movement (+/-) = (+/-) z axis translation (rot, scale)
ctrl + right mouse + movement (+/-) = (+/-) x axis translation (rot, scale)
ctrl + left and right mouse + movement (+/-) = (+/-) y axis translation (rot, scale)

and these transforms are done in world coordinates, left movement is negative, right movement positive axis, up, down, etc.
I dont know where you want to go with your project, but placing an AABB around each object, creating a pick ray with mouse left click, checking for hits against the object bounds (shift click selects multiple objects, then add them to a list) and then using the mouse/key combo might move you along quicker then trying to select and identify the manipulator axis that was hit.
Unless of course you are going for something more along the lines of what Maya/3D Studio does.
Either way I guess the initial object selection has to be done, either by unprojecting, or using a pick ray against the object.
BlackDuck
BlackDuck
All excellent advice. Thank you and I will code away. Till the next hurdle.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.