Drag&Drop of objects

Hi @ all,

I got a little problem, I want to drag an OpenGLobject and drop in at another postion! What I don’t know is, how can I find out how many units I have to translate the object according to the units I have moved the mouse.

A little example:

  • I drag the object with the mouse at the windowcoordinates
    x=500 and y = 480
  • I now move the mouse 50 units in x-direction.
  • so I want drop the object at x=550 and 480

-> how many units in OpenGLcoordinates do I have to translate the object

I’m looking forward to your replies…

p.s.: if I didn’t express clearly please post !!!

Hi,

You should consider using gluProject method to convert your new mouse position to view position.

Thanks for your reply!

I tried to use the gluUnProject method but I’m not sure about some of the parameters.

WinX,WinY and Winz are the window coordinates which should be maped. But what do I have to put into the following three parameters. Or how do I get my modelview-, projectionmatrix and the viewport which apperently seem to fit into those parameters? Futher on how do I get the maped coordinates out of the last three parameters?

// ----------------------------------
// Unprojection from window to world
private boolean     unProjectFlag   = false;
private int         mouseX, mouseY;
private double[]    worldX          = new double[1];
private double[]    worldY          = new double[1];
private double[]    worldZ          = new double[1];

private double      model_view[]    = new double[16];;
private double      projection[]    = new double[16];
private int         viewport[]      = new int[4];

gl.glGetDoublev ( GL.GL_MODELVIEW_MATRIX, model_view );

    gl.glGetDoublev (GL.GL_PROJECTION_MATRIX, projection);
    gl.glGetIntegerv (GL.GL_VIEWPORT, viewport); 

This code will help you get the parameters for gluUnproject
the results are in the worldX, worldY & worldZ arrays…

Thanks so far! I tried it out, but
gl.glGetDoublev ( GL.GL_MODELVIEW_MATRIX, model_view ),
gl.glGetDoublev (GL.GL_PROJECTION_MATRIX, projection), and gl.glGetIntegerv (GL.GL_VIEWPORT, viewport) return arrays whose elements are all 0.0! I think I’ve placed the gluUnProject at the wrong position! Can you please tell me where I have to call it?

p.s.: Sorry, but I’m a bloddy beginner :-[

Make sure you do all your glGet* operations after you’ve set your projection and camera matrices up. Either way though, you shouldn’t be getting all 0’s unless you’re doing something wrong. Odds are you’re making gl calls in a mouse event thread - naughty!

As usual Orangy Tang is right.

You MUST call the glUnproject with a valid gl Context.
May i suggest you to use a flag and do it in your display method…

Ok, now it works… nearly :-/

The objects are translated, but along the wrong axis!
So if I move the mouse rightwards the object is translated downward and the same thing with the y-axis! If I move the mouse upward the object is translated leftwards !!! ???

What the hell is wrong with that ???

Don’t forget that point x=0 y=0 is bottom-left for openGl and top-left for swing…

So if you don’t take care of this is quite normal that up is down and vice versa.

But the axis switching is really strange…

Following should work :
glu.gluUnProject ( mouseX, mouseY, mouseZ
model_view, projection, viewport,
worldX, worldY, worldZ );

Be sure to set viewport properly…

I would recommend you look at the gleem library that is contained in the jogl-demos workspace. As long as you can feed it the appropriate camera parameters, it takes care of all of the linear algebra required to get precise dragging of objects using Open Inventor-style manipulators. gleem is used for all of the mouse interaction in the JOGL demos.

Ok, thanks for all those hints ;D

It now works properly! But how can I recognize when the dragging is over? What I mean is the following:

If I want to drag an object I click on it with the mouse and move it while pressing the mousebutton. If the object is at it’s destination I release the mousebutton. Now the dragging is over. But how do I recognize this event?

Sorry for asking such stupid questions :-[ :-[ :-[

You certainly can usethe MouseReleased flag of the mouse event for your dragging end operation and the corresponding method from the listener don’t you:P