# Thread: Moving objects with mouse in 3D.

1. ## 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!

2. 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. 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

4. 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?

5. 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.

6. 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.. ??

7. 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.

8. 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. 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. 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. You never assign winZ.

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

12. 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. 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);`

14. 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?..

15. ## move object in 3D

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