PDA

View Full Version : drag object with mouse?



detzel
11-08-2002, 08:36 AM
I need to be able to drag an object under the mouse. http://oz.plymouth.edu/~r_detzel/window.jpg thats my scene and I've read a few post about this but I still can't figure out how to do it.

John Jenkins
11-08-2002, 08:48 AM
Well how I would do it with VB is.. I would use the Form_mousemove function. This function detects a mouse moving over a form and provides the x and y position in terms of pixels. Then all you would have to do is determine if the mouse was moving up or down by its initial and final position. Then you can write code that translates the X Y coordinates into OpenGL screen coordinates and use glTranslate3f to move the object. Oh also if you want to just move one sphere you will have to push pop matrix so that only one thing moves and not the whole screen.

detzel
11-08-2002, 09:38 AM
I tried that but what happens is I move the mouse say 20 pixels and if I translate that much on the scene then it does look right....I need the mouse cursor to stay on the object.

John Jenkins
11-08-2002, 10:11 AM
if you move 20 pixels you don't want to gltranslate3f 20,0,0 . The scale is all wrong. What you should do is draw to objects a known distance apart. Then hover your mouse over each one and write down what the pixel readings are. the ratio of the difference of the pixels over the difference of the two objects will help you with that.

GlynnC
11-10-2002, 12:54 PM
1. Use gluProject on the object's reference point to convert it to window coordinates. Discard the X and Y coordinates, and keep the Z coordinate.

2. Use gluUnProject with the starting mouse X/Y coordinates and the Z coordinate from step 1 to get a starting 3D object-space position.

3. Repeat step 2 for each mouse coordinate throughout the drag.

You want to keep the position of the object's reference point relative to the mouse coordinate constant, so compute:

O' = O + P' - P

where:
O is the object's initial position,
P is the 3D projection of the initial mouse position (from step 2),
P' is the 3D projection of the current mouse position (from step 3), and
O' is the object's new position.

zed
11-10-2002, 07:24 PM
a bit of clarification to the previous post
(this is also answered in the faq)

1/ read the depth value of the pixcel under the cursors position onscreen
2/ use gluUnProject with the cursors x,y onscreen position as well as the depth value from 1 (above)
3/ this will return the 'world' position of the pixel under the cursor
4/ translate the object to that position

DanDanger
11-12-2002, 10:12 AM
Hi anyone,

my Problem ist very excatly Detzel's Problem.
I get my Mouse-Coordinates with :

void GetMouseCoord(int PosX, int PosY) {
// PosX and PosY are the X and Y Coordinates // from the Mouse !

GLdouble PosZ ;
GLdouble objx, objy, objz;
GLint viewport[4];
GLdouble modelMatrix[16];
GLdouble projMatrix[16];

glReadPixels(PosX, PosY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &PosZ);

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);

gluUnProject((GLdouble)PosX, (GLdouble)PosY, PosZ, modelMatrix, projMatrix, viewport, &objx, &objy, &objz);

Move_Obj_X = (objx) ;
Move_Obj_Y = -(objy) ;
Move_Obj_Z = (objz) ;
}

But the Coordinates from objy, objy and objz are all messed up ... :-(

When i move the Sphere to the Move_Obj_X,
Move_Obj_Y and Move_Obj_Z _ Coordiantes,
this point is far away from the mouse.

Any suggestion's what's wrong with my code ??

@Detzel : Who did you finnaly realized your Drag and Drop ???


Thank's
DanDanger

zed
11-12-2002, 10:32 AM
normally for os's (0,0) is at the top left, opengl wants it at th bottom keft
thus use window_height - y (for readpixels)
going y = -y is not the same thing

DanDanger
11-12-2002, 12:31 PM
@zed :
Hi zed. Thank's for your answer, but the X-Value is messed up too. When i moved the Mouse just a few Pixels on the x-Coordinate,
the objx, Value (and thus, my "Drag and Drop" Object, too) are totally out of the screen.

Almost Every Task on Drag and Drop (or Mouse-Position) in this Forum is about gluUnProjekt, but it doesn't semm to work ;-(

BTW : I'm using SDL under Linux to Obtain the Mouse-Window-Coordinates.
Is it possible, that SDL retunr "other" Window-Coordinates then glUnProject "expects" ????

Greeting's
DanDanger

zed
11-12-2002, 07:46 PM
i email u(from your profile) with a working demo