Proportional speed movement between mouse and cube

Posted by user1350772 on Game Development See other posts from Game Development or by user1350772
Published on 2013-10-03T17:51:17Z Indexed on 2013/11/03 22:21 UTC
Read the original article Hit count: 294

Filed under:
|
|

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

EDIT 1 cube

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.

© Game Development or respective owner

Related posts about c++

Related posts about opengl