PDA

View Full Version : Cylinder rotation

heygirls_uk
07-03-2004, 04:05 AM
I'm attempting to translate a cylinder so that its center is at the origin, and then make it rotate. However, the origin appears to be
moving with the translation so that the leftmost end of the cylinder is always at the origin. How can I rotate the cylinder with its center at the origin? Is it possible to rotate it within its own local coordinate space (i.e. its center already at local origin)?

The code I'm using is:

Gl.glPushMatrix();

//translate center of cylinder to origin
Gl.glTranslatef(-1.25f, 0.0f, 0.0f);
Gl.glRotatef(a, 0.0f, 1.0f, 0.0f);

Gl.glTranslatef(0.0f, 0.0f, 0.0f);
Glu.gluCylinder(q1, 0.6, 0.6, 2.5, 25, 25);

Gl.glPopMatrix();

Gl.glFlush(); Thanks for any suggestions

plasmonster
07-03-2004, 06:26 AM
By default, the cylinder is oriented along the z-axis. I just switched the x and z components in your first Tranlatef() call, and removed the 2nd (superfluous) Translatef() call. I also named all the constants for easy reading.

float height = 2.5;
int slices = 25;
int stacks = 25;
float a = ???;

Gl.glPushMatrix();

// Are you sure you want an identity here?
// Try it without this. If the camera is at the
// top of the stack, you'll want to remove
// this line.

// Rotate at origin 2nd.
Gl.glRotatef(a, 0.0f, 1.0f, 0.0f);

// Translate to origin 1st.
Gl.glTranslatef(0, 0, -height/2);

Gl.glPopMatrix();
Gl.glFlush(); edit:

Changed wording.

heygirls_uk
07-03-2004, 06:50 AM
Thanks, that worked a treat.

When you say the cylinder is oriented in the z-axis, how do I set it up to be oriented in the x or y axis? (This is all pretty new to me!)

Cheers for the help

plasmonster
07-03-2004, 07:09 AM
There's some info on quadrics here:

http://www.parallab.uib.no/SGI_bookshelv....html#id5518152 (http://www.parallab.uib.no/SGI_bookshelves/SGI_Developer/books/OpenGL_PG/sgi_html/ch12.html#id5518152)

To orient quadrics, or anything else for that matter, just use the basic transformations. glRotate*(), glTranslate*(), and glScale*() can be used to achieve any orientation you like.

For example, to orient the cylinder along the x-axis, you might do this:

...
glRotatef( 90, 0, 1, 0 );
glTranslatef( 0, 0, -height/2 );
...Or, to orient along the y-axis, you might do this

...
glRotatef( 90, 1, 0, 0 );
glTranslatef( 0, 0, -height/2 );
...All this could be in addition to any other transforms you might want. The important thing is to understand that the order of transformations is very important.

You may find this chapter of the Red Book helpful:

http://fly.cc.fer.hr/~unreal/theredbook/chapter03.html

heygirls_uk
07-03-2004, 09:12 AM
Thanks for all the info, it's making sense now