mouse dragging

Hi ,

I’m trying to implement a mouse dragging that must move the entire scene. I’m working in 2D.
There was someone here on this forum that gave me the idea to store the mouse coordinates when one of the buttons is pressed (old coordinates) and to store also the coordinates of the mouse when it is dragged (current coordinates). I’m taking the difference between the current and the old coordinates and I’m applying it in the glTranslate function.
Here is some code :


// this is in my display()
gl.glPushMatrix();
        	
        	gl.glTranslated(scrollX/zoomW, scrollY*zoomH, 0);
        
        	gl.glGetIntegerv(GL.GL_VIEWPORT, v, 0);
    		gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, m, 0);
               gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, p, 0);
            grapheRe(drawable, showRe, divRe);
	    	grapheIm(drawable, showIm, divRe);
	    	drawGrid(drawable, showGrid, divRe);
	        drawXAxis(drawable);
	        drawYAxis(drawable);
gl.glPopMatrix();	    

//mouse handler for mousePressed
void mousePressed(MouseEvent e){
   if (e.getButton() == MouseEvent.BUTTON1){
            x = e.getX();
           y = e.getY();
   }
}
//mouse handler for mouseDragged

void mouseDragged(MouseEvent e){

                double cur[] = new double[4];
		double old[] = new double[4];
		
                //m, p and v are initialized in the display()
                glu.gluUnProject((double) e.getX(), (double) (canvas1H-1)-e.getY(), 
				0.0, m, 0, p, 0, v, 0, cur, 0);
		glu.gluUnProject((double) x, (double) (canvas1H-1)-y, 
				0.0, m, 0, p, 0, v, 0, old, 0);
		
                double distX = cur[0] - old[0];

                //scrollX is global; used in glTranslate 
               scrollX +=distX;
}

At this moment I’m trying to do the drag only in the X direction.
The problem with this method is that the dragging is not proportional to the length of the mouse drag.
Any ideas how to make it proportional ?

Regards,

Anton

length = sqrt(xx + yy);

Hi keldon85,

I’m sorry but I can’t understand very well what you mean by making length = sqrt(xx + yy)

What exactly are x and y and where I have to use the var length ? Can you give me some more details?

Regards,

Anton

Hi, I don’t think you’ll need calculate the length of the mouse offset.
But if I am right you should perform the unproject computation with a valid GLContext, that is to say not in the EDT in which you must be in your mouseDragged code.

You can also compute your move yourself without unproject method since you are in a simple 2D world…

I think it’s because you’re continuously adding the difference between the original mouse position and the new one. So if the mouse started at x=5 and is now at x=8 you add 3 but if in the next event is has moved to x=10 you add 5 even though it has only moved 2 units.

This is because I didn’t explain very well my original idea. Instead of directly changing scrollX from within the mouseDragged() method I would introduce a new variable, let’s say dragX which you initialize to 0.

In the display() method you do: gl.glTranslated((scrollX + dragX)/zoomW, scrollY*zoomH, 0);

while in the mouseDragged() method you say: dragX = distX;
(not using += here!)

and finally in the mouseReleased() you add: scrollX += dragX; dragX = 0;

why do it like this? and not just calculate the distance the mouse travelled between each mouseDragged-event? Because this tends to become very imprecise when the user makes wild movements.

Hope this helps.