# Proportional speed movement between mouse and cube

Hi i´m trying to move a cube with the freeglut mouse "glutMotionFunc(processMouseActiveMotion)" callback, my problem is that the movement is not proportional between the mouse speed movement and the cube movement.

MouseButton function:

#define MOVE_STEP 0.04
float g_x=0.0f;

glutMouseFunc(MouseButton);
glutMotionFunc(processMouseActiveMotion);

void MouseButton(int button, int state, int x, int y){
if(button == GLUT_LEFT_BUTTON && state== GLUT_DOWN){
initial_x=x;
}
}


When the left button gets clicked the x cordinate is stored in initial_x variable.

void processMouseActiveMotion(int x,int y){
if(x>initial_x){
g_x-= MOVE_STEP;
}else{
g_x+= MOVE_STEP;
}
initial_x=x;
}


When I move the mouse I look in which way it moves comparing the mouse new x coordinate with the initial_x variable, if x>initial_x the cube moves to the right, if not it moves to the left.
Any idea how can i move the cube according to the mouse movement speed? Thanks

The idea is that when you click on any point of the screen and you drag to the left/right the cube moves proportionally of the mouse mouvement speed.

• Orthographic or perspective projection? In general, screen coordinates (the mouse) are not directly related to world coordinates (the cube) and you'll have to transform points/vectors between the two vector bases. Oct 3, 2013 at 18:11
• perspective projection Oct 4, 2013 at 8:16