# Moving objects with mouse in 3D.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-05-2006
psychopath
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!
• 09-05-2006
linuxdude
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)
• 09-06-2006
hdragon
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
• 09-06-2006
VirtualAce
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? :)
• 09-06-2006
psychopath
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. :)
• 11-22-2006
pabzea
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.. ??
• 11-22-2006
psychopath
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.
• 11-23-2006
VirtualAce
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.
• 11-23-2006
pabzea
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.
• 11-23-2006
pabzea
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#
• 11-23-2006
Perspective
You never assign winZ.

If you want to do picking, have a look here http://nehe.gamedev.net/data/lessons....asp?lesson=32
• 11-24-2006
pabzea
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. =(
• 11-24-2006
psychopath
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);`
• 11-28-2006
pabzea
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?..
• 07-10-2011
Ogbobe Buko
move object in 3D
Quote:

Originally Posted by psychopath
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last