# Shape rotation issues

Ok, im a noob having fun experimenting with opengl for the first time over the past week or so. I’ve run into an issue with rotating shapes I cant seem to get around.

I’ve drawn a simple shape and want to rotate it around itself (i.e i want its ‘pivot point’ centered on the shape, in 3d program speak). When rotating my shape with,

float angle = 30;
glRotatef(angle,1.0f,0.0f,0.0f);

the shape flys around the screen in the x (instead of on the spot), and with a small function i created to +=10 to angle the shape eventually flys back to its original position and begins its journey again.

I followed some tute that said I have to use glTranslate to move to the point I wanted to rotate from, so I moved to the center of my object with

glTranslate(1.5f,-1.5f,-5.0f);

before drawing but this made little difference and my shape still rotates around this ‘invisible’ point ?

I know im missing something, probably small any help ?
confused noob
thanks

Ok i’ve read another tute that states,“In order for your object to spin around an axis, it has to be designed AROUND that axis. You have to remember that the center of any object should be 0 on the X, 0 on the Y, and 0 on the Z.”

I kind of get that

it then gave me some example code of a triangle,

``````glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
``````

which i used to replace my square, and everything worked great… i.e the triangle spun in place… so I assume its the WAY im drawing my square that’s screwing up my code

so can anyone tell me how I am drawing my square incorrectly in regards to the above comment and original problem ? i.e designing my square around the axis i want to spin it around ?

``````glVertex3f(1.0f, -1.0f, -5.0f);
glVertex3f(2.0f, -1.0f, -5.0f);
glVertex3f(2.0f, -2.0f, -5.0f);
glVertex3f(1.0f, -2.0f, -5.0f);
``````

much thanks

It is entirely possible that you may not completely understand this, since the example code deals with shaders and you’re using the fixed-function pipeline. But the theory is the same either way.

Not fully sure of this, but try:

The center of your square is 1.5, -1.5, -5.0.

Rotations happen around 0.0, 0.0, 0.0, so translate by -1.5, 1.5, 5.0 (this puts the center of your square at 0,0,0) then do your rotation, then translate back by 1.5, -1.5, 5.0 to put your square back in place.

The translations and rotations are done on the modelview matrix stack.

With regard to designing your square, it’s origin (the point you want to rotate it around) needs to be 0,0,0. So your vertices “should” be:

-1,1,0
1,1,0
-1,1,0
-1,-1,0

but then you’d need to translate by 1.5,-1.5,-5.0 to put the square where you wanted it.

The difference would be when your object is designed in its own space/coordinate system, then you can just rotate and then translate it to where you want its origin to be in the world. If it isn’t (because you’ve defined it in world coordinates, which is what you’re doing), then you need to translate, then rotate, then translate back.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.