# OpenGL Moving object in 3D space

This topic is 2599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a small application that load simple objects in a opengl viewport and allows me to select each object interactively clicking on it (I'm using opengl picking tecnique).

Once an object is selected, I'd like to move it around, always using the mouse. That works fine if the camera is not moved: if I move the mouse horizontally, the object is translated on the X axis; if I move the mouse vertically, the object is translated on the Y axis.
However, when the camera is rotated, for example, to top view, axis are rotated as well and when the mouse is moved vertically it will have to translate the object on the Z axis, in this case.

So, how would I calculate the transformations so that I can apply mouse xPos and yPos properly?

Thanks again for any help!

##### Share on other sites

Given the fact that m1 (mouse screen coordinates) and p1 (projected coordinates in 3d space of the picked object) are known, I think I might solve this problem by:

1) calculating a plane perpendicular to the m1->p1 vector
2) project a ray m2->p2 (where m2 is the mouse screen coordinate of the new point) and calculate the intersection with such plane. That should give me the p2 coordinates.

In my project I already have a vector class, and I know how to use glReadPixels: but I don't know how to achieve the above two points. Could someone please help? Thank you very much.

##### Share on other sites
the camera has right, up, forward vectors, translate your object along those vectors

##### Share on other sites
Ok but how would that work, if I don't know the p2 coordinates? I mean, by what quantities you move the object along those vectors?

##### Share on other sites
Hidden
if I move the mouse horizontally, the object is translated on the X axis;[/quote]
X-axis of the camera, even if x = 1,0,0

However you make your camera, do the exact operations. Or calculating the matrix inverse. You can find functions online for matrix inverse. For this case all you need to do is transpose the camera matrix to compute the cameras inverse.

[0 4 8 12]
[1 5 9 13 ]
[2 6 10 14]
[3 7 11 15]

ApplyCameraMatrix
glGetDoublev(GL_MODELVIEW_MATRIX)

//Position += cameras x vector in the world(which is the inverse of the above matrix)
Position.x += matrix[0]
Position.y + = matrix[4]
Position.z += matrix[8]

A point in 3D (x,y,z) will map to where your mouse is. So you do the opposite and figure out where the mouse x,y, and some z component will map back into the 3d world.

Use glUnproject, to get the mouse x,y into the world. When you Unrpoject use the mouse x, mouse y, and z-depth of the object you want to move, then Unproject your new mouse moved x, y, z-depth of the object, and that is the new position of the object.

##### Share on other sites
I wish it was so simple. Take a look at the attached images: in the first one, I just picked the object a 0,0,-10. As I drag it, the object z-depth is going to the end of the far plane (-800). I honestly don't know if there is a way to overcome to this, without delving into creating a parallel plane to camera, testing ray-intersection etc.

##### Share on other sites
You have to actually read the final z-depth buffer value to know where that object is relative to your camera.

So x,y,z of object in world renders to = screenx,screeny,screenz (get screenz by reading the depth buffer glReadPixels(GL_DEPTH_COMPONENT))

if you click your mouse say on the right edge of the screen, the new x,y,z of the object is:
gluUnproject(mousex, mousey, screenz) (screenz because since you suggested a parallel plane would imply your objects z from camera is going to remain the same if you move the object left/right up/down).

##### Share on other sites
Thanks Adam, I'm going to try as you say. BTW, I've seen on your web site that you are developing a flight simulator. Good luck with it! I also developed one some years ago, when my mind was younger and my opengl/math not so rusty as today (if you are curious watch this movie:
).
Should you need some info or help about how to code the flight model, tell me...
Cheers

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015830
×