2
\$\begingroup\$

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 http://oct.imghost.us/S1lC.png

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.

\$\endgroup\$
2
  • 5
    \$\begingroup\$ 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. \$\endgroup\$ Oct 3, 2013 at 18:11
  • \$\begingroup\$ perspective projection \$\endgroup\$ Oct 4, 2013 at 8:16

1 Answer 1

1
\$\begingroup\$

The easiest "sure fire" method of doing this that I know of is to project rays from the screen into the world, just like the various picking articles on this site talk about.

You can project a ray onto a surface upon which the cube is meant to move. In the simplest case this may just be the XY plane at Z=0. It could be a plane at a non-screen-aligned angle, like the "ground" XZ plane. Or it could be any arbitrary surface, like a heightmap or curved surface or polygonal surface.

I found it best when dragging and moving the mouse to shoot two rays. Take the original mouse position before the move and then the mouse position after the move. Project both rays into the scene. Form a vector from the pre-move projected point to the post-move projected point. Move the dragged object by that vector. This allows you to grab any part of the object - not just the center - and have the drag movement "feel right." If your surface is not a flat plane you'll need to snap the object to the surface (or project many points along its surface between the mouse move vector) in order to have the dragged object follow the surface visually and look nice.

Alternatively, if you're not dragging, but just want a fixed point on an object to follow the mouse, you need only project the single new mouse position and move the object there.

You could also use either interpolation or some super basic physics if you want the box to "chase" the mouse rather than instantly move along with it. If you go this route and want the movement to accelerate/decelerate, I highly recommend the physics approach rather than interpolation as interpolation doesn't work too well if the interpolated path might change in mid-interpolation, but the physics will just work as expected usually.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .