 # Body Centred and World Centred Rotations

I’m trying to do a body centred rotation (where x-y-x are stuck to the body) and a World Centred rotation (where the x-y-z axis are stuck to the world and don’t rotate).
Here is my attempt:

• Push View Matrix
• do translation
• do trackball (if selected trackball)
• Find current_x_angle (or y-angle or z-angle depending on key pressed).
• glRotate (current_x_angle,1.0,0.0,0.0);
• glRotate (current_y_angle,0.0,1.0,0.0);
• glRotate (current_z_angle,1.0,0.0,1.0);
• do scaling
• draw the thing
• Pop the View matrix

well, the thing somehow doesn’t work well. The x-y-z axis don’t rotate as they should (ie, doing a y-rotation doesn’t rotate the x axis…).

and I don’t know how to make the x-y-z axis stick to the screen and don’t rotate, but I’ll figure out.

anyways, I’ve tried all sort of things to get the xyz axis stuck to the body and rotating. Once I got it working, except translation stopped working.

I need help.

make sure your model is modeled with its local coordinates around 0,0,0

glPushMatrix();
glTranslated(xpos,ypos,zpos); // world
glRotate (current_x_angle,1.0,0.0,0.0);
glRotate (current_y_angle,0.0,1.0,0.0);
glRotate (current_z_angle,1.0,0.0,1.0);

// draws a quad on the X/Z plane
glBegin(GL_TRIANGLE_STRIP); // Build Quad From A Triangle Strip
glVertex3d(0+size,0,0+size); // Top Right local
glVertex3d(0-size,0,0+size); // Top Left local
glVertex3d(0+size,0,0-size); // Bottom Right local
glVertex3d(0-size,0,0-size); // Bottom Left local
glEnd();

glPopMatrix();

Thanks Dan,

Makes sense, but that doesn’t fix my problem. Perhaps I didn’t explain it well…here it is again:

void draw_scene ()
{
glPushMatrix();
do_transformations();
draw_thing();
glPopMatrix();
}
/---------------------------------------------------------------------/
void do_transformations()
{
do_translation();
do_rotation();
do_scaling();
}
/--------------------------------------------------------------------/
void do_trasnlation()
{
glTranslatef (x_trans, y_trans, 0.0); //don’t have code with me, but I think this is correct
}
/--------------------------------------------------------------------/
void do_rotation()
{
glMultMatrixf (trackball_matrix);

glrotatef (x_angle, 1.0, 0.0, 0.0);
glrotatef (y_angle, 0.0, 1.0, 0.0);
glrotatef (z_angle, 0.0, 0.0, 1.0);

glutPostReDisplay();
}
/--------------------------------------------------------------------/
void do_scaling()
{
glScalef (…)
}
/-------------------------------------------------------------------/
void draw_thing()
{
glWiredPot…
}

so this is the basic code, much of it was given to us since we just started opengl a 2 weeks ago. Translation had already been coded. Now Rotation works, somewhat, but not quite right. All movements are controlled by keyboard and mouse. So if I hit x and move mouse when left button is pressed, object moves in x direction and so on. so an x-y-z rotation actually consists of 3 separate movements.

hit x -> move mouse -> let go mouse button
hit y -> move mouse -> let go mouse button
hit z -> move mouse -> let go mouse button

the problem is obvious when I do an x-y-x rotation. 1st object moves around x-axis, then y-axis, but the 3rd rotation is around the OLD x-axis, not the rotated x-axis. This is not all, if I do a z-x-z rotation things get more complicated as now the z-axis actually does get rotated. In fact, in the do_rtation() function, the last glRotate* call (in other words, the one 1st applied to model view matrix) does get rotated. I came with a solution as following

void do_rotation()
{
glMultMatrixf (trackball_matrix); // ignore this, makes no difference
glPushMatrix();
glLoadMatrixf (old_matrix); //note that old_matrix must be init or else screw up

glrotatef (x_angle, 1.0, 0.0, 0.0);
glrotatef (y_angle, 0.0, 1.0, 0.0);
glrotatef (z_angle, 0.0, 0.0, 1.0);

glGetFloatv (GL_MODELVIEW_MATRIX, old_matrix);

glPopMatrix();

glMultMatrixf (old_matrix);
}

well, there was an initial problem of the object appearing with half the scale…a glScalef (2.0,2.0,2.0) did fix this.

and now the coordinates are actually hooked to the object. all rotation (specially xyx and zxz) worked fine, but now translation didn’t work at all (scaling does). I used a similar code to fix translation (figured I was discarding the translation matrix effect). Now there is a new problem, translation occurs in the “direction” of the axis. so if I rotate the x-axis up, then doing an x-traslation moves the object in y direction. if y axis is in z direction, object moves in z-direction (back & forth and hence appears scaled as the z-axis goes into the screen)…argh…

I’ve been playing with this for the past 4 days…other things I have tried was to do a rotation like following

glrotatef (x_angle, x , y, z); // where x, y, z are the appropiate vectors rotated

I figured I could rotate the (1,0,0) vector by multiplying it by the model view matrix (and the other vectors likewise)…results were very unpredictable, the object would start acting on its own, speeding up and slowing down at times…

See if you can help me, I think I’m trying the wrong things…the problem is that, I’ve seen sample code from last year (ppl who’ve gotten full marks) and their code behaves just like mine…but the sample solution our prof (uh, yes, this is THE COOLEST 4th year comp sci course…he he he) gave us is correct.

Help me out ppl,

Much appreciated,
Reza