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

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

  1. 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

1 Like

thanks

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.

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:

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

Thanks

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.

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[44];
GLdouble proj[4
4];
GLint view[4];
GLdouble pan_x, pan_y, pan_z;

if (mouse_state == GLUT_DOWN && 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,
&pan_x, &pan_y, &pan_z);
gluUnProject((GLdouble)x, (GLdouble)y, pan_z,
model, proj, view,
&pan_x, &pan_y, &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);
glLoadIdentity();
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(&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