# Thread: dragging using mouse movement

1. ## dragging using mouse movement

hello
I need some help in dragging of object using mouse....i have searched on net but i didn't get the perfect solution..

thanks

2. ## Re: dragging using mouse movement

Following things are involved:

1. When you start the move

1.A. Record the initial position P0 where the mouse clicked, in world space (or your choice of space). Figure out the world position of the pixel under the mouse. You can either cast ray from the camera through a point on the near plane towards point on the far plane, or you can read the depth under the mouse with glReadPixels() and unproject (research gluUnProject) (mouse x, mouse y, depth) from screenspace to worldspace.

1.B. decide which axis or plane you want to move along

2. When mouse moves

2.A.*Unproject the new mouse position, this time at both near and far planes instead of at pixel depth. With these two points, you can form a ray. Now, figure out a new point P as intersection of the ray with your choice of plane, or as closest point with the axis chosen at step 1.B. For closest point test you need to figure out if you should do it in 3D or in screen space.

2.B. P - P0 is how much you should move the object

thanks

4. ## Re: dragging using mouse movement

I'm actually having this exact problem just posted it.

My problem was that i can pick the object i want to move and I can move it.

But the mouse moves much faster than the object.

Also you can zoom out or in and i want the mouse movement to relate to that.

5. ## Re: dragging using mouse movement

Hi i just wanted to break this down and try and do it but it's a bit out of my depth so i'l post what i'm having trouble with:

Originally Posted by tksuoran
Following things are involved:

1. When you start the move

1.A. Record the initial position P0 where the mouse clicked, in world space (or your choice of space).

I have the this done in screen space, as in i have the mouse X and Y location

Figure out the world position of the pixel under the mouse. You can either cast ray from the camera through a point on the near plane towards point on the far plane, or you can read the depth under the mouse with glReadPixels()

Ok the depth is 0.08823497

and unproject (research gluUnProject) (mouse x, mouse y, depth) from screenspace to worldspace.

right after using unproject i get

posX = 434.78259904792515
posY = 269.56520458764419
posZ = 15.5385215035476

1.B. decide which axis or plane you want to move along
The X axis

2. When mouse moves

2.A.*Unproject the new mouse position, this time at both near and far planes instead of at pixel depth. With these two points, you can form a ray.

posX = 434.78259904792515
posY = 269.56520458764419
posZ = 15.5385215035476

Now, figure out a new point P as intersection of the ray with your choice of plane, or as closest point with the axis chosen at step 1.B.

This is the step thats got me, I don't understand how to do this or why we do this.

For closest point test you need to figure out if you should do it in 3D or in screen space.

Do you have any code samples of this? I've never done ray casting in openGL, i'm sort of confused and stuck at this point

2.B. P - P0 is how much you should move the object

Really i just want the mouses speed to match the objects moving on one axis at a time.

Thanks

6. ## Re: dragging using mouse movement

How do you deal with this if you rotate the scene?

I got it working from some angles but others it breaks and the object just disappears.

7. ## Re: dragging using mouse movement

hello sir
i have one problem regardind mouse movement ...when i pick a object...the object will slightly displaced from it's position and then after the slight displacement the object move smoothly with mouse....

here is the code i m using plz check it out and reply me the solution
thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>

int flag=0;

void render(int x,int y);

GLfloat objectRotX, objectRotY;
int curx, cury, width, height;
GLfloat sphere_x = 0.0;
GLfloat sphere_y = 0.0;
GLfloat sphere_z = 0.0;

int mouse_state,mouse_button;

void initialize(void)
{
glMatrixMode(GL_PROJECTION);
glFrustum(-0.50, 0.50, -0.50, 0.50, 1.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -4.0);
}

void redraw(void)
{

glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);
glPushMatrix();
glTranslatef( sphere_x, sphere_y, sphere_z);
glutWireSphere(0.5,100,100);

glPopMatrix();

}

void
display(void)
{
redraw();
glFlush();
glutSwapBuffers();

}

void
motion(int x, int y)
{
GLdouble model[4*4];
GLdouble proj[4*4];
GLint view[4];
GLdouble pan_x, pan_y, pan_z;

if (mouse_state == GLUT_DOWN &amp;&amp; mouse_button == GLUT_RIGHT_BUTTON) {
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluProject((GLdouble)x, (GLdouble)y, 0.0,
model, proj, view,
&amp;pan_x, &amp;pan_y, &amp;pan_z);
gluUnProject((GLdouble)x, (GLdouble)y, pan_z,
model, proj, view,
&amp;pan_x, &amp;pan_y, &amp;pan_z);
pan_y = -pan_y;

sphere_x = pan_x;
sphere_y = pan_y;
sphere_z = pan_z;

glutPostRedisplay();
}
}

void
mouse(int button, int state, int x, int y)
{

mouse_state =state;
mouse_button =button;

}

void myReshape(int w, int h)
{
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
if(w<=h) glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
else glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);

glMatrixMode(GL_MODELVIEW);
}

int main (int argc, char **argv)
{
int i;

glutInit(&amp;argc, argv);
glutInitWindowSize(width = 500, height = 500);

glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

glutCreateWindow("envphong");
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
initialize();
glutMainLoop();
return 0;
}

u can easily observe the slight displacement

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•