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