PDA

View Full Version : Rotation and Translation



10-18-2003, 03:58 AM
Hi,
my problem is this:
I am creating 5 cubes (like the 5-th side of a dice),with GL_QUADS, 6 faces. They rotate about themselves on all 3 axis. The code looks like this:
[CODE]
//angle changes constantly,resets to 0 once >= 360
//center cube
glPushMatrix();
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
drawCube(0, 0, 0);
glPopMatrix();

//lower left cube
glPushMatrix();
glTranslatef(-8, -8, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
drawCube(0, 0, 0);
glPopMatrix();

//lower right cube
glPushMatrix();
glTranslatef(8, -8, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
drawCube(0, 0, 0);
glPopMatrix();

//upper right cube
glPushMatrix();
glTranslatef(8, 8, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
drawCube(0, 0, 0);
glPopMatrix();

//upper left cube
glPushMatrix();
glTranslatef(-8, 8, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
drawCube(0, 0, 0);
glPopMatrix();

//and drawCube looks like this:
void drawCube(float x, float y, float z)
{
glPushMatrix();
glTranslatef(x, y, z);
glBegin(GL_QUADS);
//draw faces.....
glEnd();
glPopMatrix();
}[CODE]

Now along with these rotations I would like to have the corner cubes, in pairs, make an X-like motion along the center cuber. That is the lower left rotate along the center cube like a moon, and the upper right move in the same axis (Rotate along the diagonal). I'd like the same for the upper left and lower right cubes only move along their diagonal. How shoud I rotate my cubes? If I add another rotation for, say, the lower left cube, the rotation has undesired effects and not the effect I want.

P.S. Do you know any good tutorials for explaining rotations and translations in depth?

10-18-2003, 07:38 AM
So you want to first rotate all the cubes by some value around their local origin, and then rotate the corner cubes (which have already been rotated around their local origin) around the center cube?

10-18-2003, 09:03 AM
No, not around their local origin. The center of their rotation would be the center cube and the axis would be the diagonal. (see them as a couple of moons around a planet (the planet is the center cube) only moving diagonally instead of horizontally.

10-18-2003, 09:04 AM
..cont'd: All the cubes are already rotating around their local origin. I

cg-hci_novice
10-18-2003, 12:00 PM
Am also learning so can't help much but have you tried www.nehe.gamedev.net (http://www.nehe.gamedev.net) for a tutorial.

Need help with win mgmt myself.

Good luck.

10-18-2003, 04:43 PM
Yes, I've taken a look at the rotation tutorial, but what I want isn't covered there.

nexusone
10-20-2003, 06:13 AM
If I am correct in what you want, is for each cube to rotate on it's axis.
Then have the group of cubes rotate together?

You would do this:

// Group of cubes
glPushMatrix();
glTranslate .... // the location of the group of cubes
glRotate ..... // the rotation of the group of cubes
// Draw 1st cube in our group.
glPushMatrix()
glTranslate .. // location of cube relative to other cubes
glRotate .. // Rotation of cube
drawCube();
glPopMatrix(); // End 1st cube

//repeat above for each cube.

glPopMatrix() // End of drawing our group of cubes.

You could also do this if you wanted to have a cube orbit another cube.
Hope this helps.



[This message has been edited by nexusone (edited 10-20-2003).]

10-20-2003, 12:05 PM
You came close (- 1 translation for the group). Anyway here is what I wanted to do:



glPushMatrix();
glColor3f(1, 1, 1);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
glutSolidCube(1);
glPopMatrix();

glPushMatrix();
glRotatef(45, 0, 0, 1);
glPushMatrix();
glColor3f(1, 0, 0);
glRotatef(angle, 0, 1, 0);
glTranslatef(-5, 0, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glColor3f(0, 1, 0);
glRotatef(angle, 0, 1, 0);
glTranslatef(5, 0, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();

glPushMatrix();
glRotatef(-45, 0, 0, 1);
glPushMatrix();
glColor3f(0.5, 0.7, 0.2);
glRotatef(angle*2, 0, 1, 0);
glTranslatef(5, 0, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glColor3f(0.3, 1, 1);
glRotatef(angle*2, 0, 1, 0);
glTranslatef(-5, 0, 0);
glRotatef(angle, 1, 0, 0);
glRotatef(angle, 0, 1, 0);
glRotatef(angle, 0, 0, 1);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();

Thanks for your help though. It was really useful. The main point was that I rotate the pair around the y-axis, after I have rotated the system exactly 45 degrees (-45 for the other pair).Then the corner cube needs to be translated only on the x-axis. The 45 degree rotation "compensates" a y-axis -5 translation. Problem solved! Now I need to really understand what I'm doing.

nexusone
10-21-2003, 05:07 AM
The matrix stack is not all that hard to understand.

It works last item pushed is the first item pop'd.

PushMatrix is to save the current state of the matrix into the stack, max of 16 Push's.
PopMatrix is to load back the last pushed matrix from the stack.

So we only want one group of objects to be rotated by X, but not any other objects after drawing these objects.

glPushMatrix(); // Save Matrix
glRotate...... // Rotation of group
glPushMatrix(); // Save Matrix
// We save the matrix for each object, since the object my have rotations or translations that we do not want the other object to have
glPopMatrix(); // Load Matrix so we are back to the point of our group rotation of the matrix.

glPopMatrix(); // Load matrix to state before we started on this group.


A little side note, rotations and translations work in reverse order. And the order in which you call them does make a diffrence.

Example:
glRotate
glTranslate
drawObject
In the above the object would be translated first then rotated, rotations are done around 0,0,0. This would make the object orbit around the 0,0,0 point.

glTranslate
glRotate
drawObject
In this example, the object would be rotate around it axis or 0,0,0; then translated. In this case the object would spin on its axis at some translated location.