View Full Version : Rotating a cube with the mouse.

06-07-2009, 10:53 AM
I tried using sinusoidal and cosine functions to map the mouse position to a unit sphere and rotate my cube accordingly.But the cube moves in awkward directions and is not in sync with my mouse movement.

Could anyone suggest a way to do it?

Please help.

06-07-2009, 09:12 PM
Hi Optik,

Could you please paste the critical portions of your code so we can have a look. It's hard to visualize what you want to do from your brief description.

06-08-2009, 01:16 AM
Here is the pseudo code:

void render(void) // display callback


glutSolidCube(side); //or whatever it may be


void mouse(int mx,int my) //mouse callback
// here my approach goes like this.
// I feel I need to consider the local origin wrt the cube
// as my point of convergence of the 3 axes
// For instance if my resolution is say 800x600 and
// assume the object is at the center of my viewport which
// spans across the whole screen. then my origin wrt
// the viewing area is (400,300)(screen coordinates).
// Now when we obtain the mouse coordinates on the screen
// we find its coordinates wrt the new origin (400,300)
// by doing x = x - 400 and y = y - 300
// and assume a unit circle rotation here, ie
// anglex = arccos(x) and angley = arcsin(y)

// and I am utterly lost when it comes to z-axis rotation

PS: sorry I dont have my PC with me right now so i had to cook up this pseudo code.

06-09-2009, 12:47 PM
Here's the method I use. It works well for me.

float movement[2]; // movement in the x direction, movement in the y direction
float length = sqrt(movement[0]*movement[0]+movement[1]*movement[1]);
glRotatef(length, movement[1]/length, 0.0, -movement[0]/length);
Also, I scale the elements of movement to control the speed of the motion. I stopped using glRotatef, so you may have to play with the minus signs or swap the elements (if it's rotating backwards, or about the wrong axis).

06-09-2009, 11:31 PM
It works fine thanks, but What I need is for any fixed point of the cube to orient towards the mouse pointer all the time.