PDA

View Full Version : Problem of rotation with local axis

Thib02
12-13-2009, 10:39 AM
Hello everyone,

here is my problem,

I control the rotations of an object with the movements of the mouse. When the mouse moves horizontally, an angle is calculated and the object rotate from this angle in the x axis (horizontal). When the mouse move vertically, it's the same thing but in the y axis (vertical).

The problem is that when the first rotation (in x axis) is done, the local axis of opengl have followed the rotation of the object, and so, the second rotation is executed around the new axis y' while i want it to be executed around the former axis y !

I have looked at several forums but didn't find any simple solution (only some stuff about quaternion method but it seems very difficult and i don't have so much time).

Here is my code :

void draw_scene() {

glPushMatrix();

delta_x=lum[1]-danseuse_x;
delta_y=lum[2]-danseuse_y;

rot_x=atan(delta_x/lum[0])*180/pi;
rot_y=atan(delta_y/lum[0])*180/pi;

glTranslatef(taille_image/2,taille_image/2,0);
glRotatef(rot_x,0,1,0);
glTranslatef(-taille_image/2, -taille_image/2,0);

glTranslatef(taille_image/2,taille_image/2,0);
glRotatef(-rot_y,1,0,0);
glTranslatef(-taille_image/2, -taille_image/2,0);

draw_image();

glPopMatrix();

glutPostRedisplay();

}

If someone has a simple solution, i would be the happiest guy on earth !

ZbuffeR
12-13-2009, 04:00 PM
This is a classic.
Due to how they are accumulated, you have to reverse the order of transformations (compared for the intuive order), like this :

glTranslatef(taille_image/2,taille_image/2,0);
glRotatef(-rot_y,1,0,0);
glTranslatef(-taille_image/2, -taille_image/2,0);

glTranslatef(taille_image/2,taille_image/2,0);
glRotatef(rot_x,0,1,0);
glTranslatef(-taille_image/2, -taille_image/2,0);

By the way, this may be enough :

glTranslatef(taille_image/2,taille_image/2,0);
glRotatef(-rot_y,1,0,0);
glRotatef(rot_x,0,1,0);
glTranslatef(-taille_image/2, -taille_image/2,0);

Thib02
12-14-2009, 08:43 AM
Thank you for your answer but i think that this solution just moves the error from one axis to another, no? Indeed, the rotation will be ok for the y axis, but no for x axis because it will rotate around a new x' axis.

I was maybe not clear when i explained the problem.
I want the correction to be in real time. So when the mouse moves from, for example, the coordinates (0,0) to (5,5), the object have to rotate from K degrees around x and K degrees around y but both axis have to be the same axis that it was when the mouse was at (0,0).

MaxH
12-14-2009, 12:16 PM
Thank you for your answer but i think that this solution just moves the error from one axis to another, no? You are correct.

What you are asking for are called 'Screen Space Rotations', where your object always rotates around the axes fixed to the screen - not to the object or the rotated object. This is a more involved than stringing together some glRotates (which give you Euler Sequence Rotations). It has been discussed several times on this forum. I posted a short program that shows how to code up Screen or Object Space Rotations. You can find it at:

Screen and Object Space Rotations (http://www.mfwweb.com/OpenGL/Special_Rotations/)

Thib02
12-15-2009, 02:18 PM
Thank you a lot. I got it !