Thread: Moving objects with mouse in 3D.

  1. #1
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071

    Moving objects with mouse in 3D.

    One of the last things I need to do to make my editor to behave..well, like an editor, is allow the user to move objects around in the 3D viewport. If I were doing this in 2D, or included 2D viewports, it would be easy.

    However, I need to figure out how to translate objects by the amount the mouse was moved (the easy part), while also moving on the appropriate axis, which I guess would be dependant on the camera orientation (the hard part).

    If i'm lucky, this is no more difficult than performing some fancy vector calculation that i'm not aware of.

    Anyway, thanks!
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    well if you think you could take the distance mouse moved at an angle for y-axis then you can figure out the perpindicular normal and rotate by a certain distance (however sensitive you want)

  3. #3
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Or u can just let the mouse go up and down, then if u want it go in, press a button and move the mouse
    Hello, testing testing. Everthing is running perfectly...for now

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It depends on how you have your editor setup. If you have 3 views then the meaning of X,Y, and Z change depending on the view point.

    Blueprint style - 3 views
    TOP:
    - Mouse left/right = movement along X
    - Mouse up/down = movement along Z

    FRONT:
    - Mouse left/right = movement along X
    - Mouse up/down = movement along Y or along Z (could be either)

    RIGHT:
    - Mouse left/right = movement along X
    - Mouse up/down = movement along Y

    1 large 3D view
    If you have 1 big view then you are going to have to do something like pressing button 3 while dragging the mouse always moves along Z. A nice thing to have as well is a way to LOCK the axes of movement so that users can manipulate their models easier. Most modelling programs allow you to lock out X, Y, Z etc, etc. This is handy for fine tuning vertex positions while being assured the mouse will only move the vertex on one axis.

    Unproject mouse position
    Since the mouse only moves in 2D the only way to actually determine where it is in world space is to untransform (IE: unproject) the mouse coords by the same transformation matrices as the view. However this will not be easy to use and is not an intuitive interface for modelling. But you could accomplish this by using one matrix that represents the final complete unproject/untransform and multiply the mouse coords by this. Again, not a good idea.

    Use a control in the control bar
    The best way for one view is to allow the user to select which axis the mouse will manipulate.

    A simple control would suffice:

    X - [Mouse X]* ===> combo box with None,Mouse X,Mouse Y,Mouse Z
    Y - [None]* ===> combo box with None,Mouse X,Mouse Y,Mouse Z
    Z - [Mouse Y]* ===> combo box with None,Mouse X,Mouse Y,Mouse Z

    Notice that with this approach you also gain the locking ability w/o adding code. To lock an axis, simply set the axes you don't want to move to None. Just stick this little control on your control bar at the top and your users will thank you millions of times for it.


    Like my text-based combo boxes?
    Last edited by VirtualAce; 09-06-2006 at 12:44 PM.

  5. #5
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Ok, that all sounds good, but i'm still unsure of something.

    In my engine, the axis are:
    +X - Right
    +Y - Up
    -Z - Into the screen

    Now assuming the movement is restricted to the X and Z, and the camera is oriented facing the screen, it would be simple enough to have X mouse movement control the X axis, and Y mouse movement control the Z. However if I rotate and/or move the camera so that it's looking down the X (-X into the screen), the mouse control would have to be changed to reflect the new camera orientation, with the mouse X controlling the Z, and mouse Y controlling the X. To put it simply, I have really no idea how the math works to calculate that (and of course, i'm not just snapping the camera to an axis all the time, it would have to interpolate).

    Thanks again!

    EDIT: and yes, the text-based combo's are very slick Bubba.
    Last edited by psychopath; 09-06-2006 at 04:21 PM.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    I know a long time passed before the last post... but I want to know if some1 have a code for moving objects with mouse in 3d...

    pls... post here..

    thx.. bye

    Edit. In fact how to recognize the 3d coord of the mouse click.. ??
    Last edited by pabzea; 11-22-2006 at 05:22 PM.

  7. #7
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    Well, I have code for such things, as I got it working since I started this thread. But i'm not posting it just because you asked for it.

    Make some effort at doing it yourself, and i'll be glad to help if you get stuck.
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You need this:

    Right vector 1.0f,0.0f,0.0f
    Up vector 0.0f,1.0f,0.0f
    Look vector 0.0f,0.0f,1.0f

    The rest is transforming those vectors.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    hey men... i understad what u said... actually i have some code, but i was not really sure... im gonna try it.. and if i got stuck, i know u will help me..

    thx.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    this is my code... plz.. what am i doing wrong??..

    Code:
    	protected override void OnClick(EventArgs e)
    	{
                int x = MousePosition.X;
                int y = MousePosition.Y;
                
                int[] viewport = new int[4];
                double[] modelview = new double[16];
                double[] projection = new double[16];
                float winX, winY, winZ;
    
                unsafe
                {
                    double posX, posY, posZ;
    
                    GL.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelview);
                    GL.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection);
                    GL.glGetIntegerv(GL.GL_VIEWPORT, viewport);
    
                    winX = (float)x;
                    winY = (float)viewport[3] - (float)y;
                    GL.glReadPixels((int)winX, (int)winY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, &winZ);
    
                    GL.gluUnProject(winX, winY, winZ, modelview, projection, viewport, out posX, out posY, out posZ);
    
                    MessageBox.Show(posX.ToString() + "," + posY.ToString() + "," + posZ.ToString());
                }
    	}
    its a simple function on the click event.. and it prints the 3d coords.
    Edit.. Btw, im using OpenGL with C#
    Last edited by pabzea; 11-23-2006 at 06:57 PM.

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    You never assign winZ.

    If you want to do picking, have a look here http://nehe.gamedev.net/data/lessons....asp?lesson=32

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by Perspective
    You never assign winZ.

    If you want to do picking, have a look here http://nehe.gamedev.net/data/lessons....asp?lesson=32
    i watched that tutorial... but in that case.. he has a ortho view, so it doesnt matter the z coord..
    my problem is about z coord.. or maybe i'm wrong.. and i dont understand at all. =(

  13. #13
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,071
    gluUnproject()'s first three arguments aren't window coords, it's the mouse position.

    Try:
    Code:
    GL.gluUnProject(Cursor.Position.X, Cursor.Position.Y, 0.0, modelview, projection, viewport, out posX, out posY, out posZ);
    M.Eng Computer Engineering Candidate
    B.Sc Computer Science

    Robotics and graphics enthusiast.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    using my code it's almost ok.. the 0,0,0 coord is ok... but when I click in another place, for example: 0.4, 0.3, 1.5 and then I do a rotate.. my coords should change.. but they dont =(...

    humm.. also, my 0,0,0.. really is: 0,0,4.999999992549419

    why z coord = 4.999999992549419 ?? it would be "0"... =(

    why the coords dont change when I apply a rotate?..
    Last edited by pabzea; 11-28-2006 at 10:21 AM.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    move object in 3D

    Quote Originally Posted by psychopath View Post
    gluUnproject()'s first three arguments aren't window coords, it's the mouse position.

    Try:
    Code:
    GL.gluUnProject(Cursor.Position.X, Cursor.Position.Y, 0.0, modelview, projection, viewport, out posX, out posY, out posZ);

    hi, i am having the same problem, attempted to use the gluUnproject, but its not working, i have 6 models loaded, any ideas what i can do...

    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to rotate child objects in 3D
    By Arianiv in forum Game Programming
    Replies: 11
    Last Post: 04-03-2008, 05:09 AM
  2. Is there an example of picking objects using mouse?
    By salman86 in forum Game Programming
    Replies: 2
    Last Post: 08-14-2005, 03:23 PM
  3. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  4. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 07:01 PM
  5. mouse click coords and moving a rect
    By techrolla in forum Windows Programming
    Replies: 1
    Last Post: 03-07-2004, 09:49 PM