Hi there,
i have this rectangle and i want to be able to rotate this shape around another when i press a key.

i have been able to make it rotate where it is.
There are two problems i am having difficulties with:

- I can get the shape to rotate by placing glRotatef(angle,x,y,z); within the creation of the rectangle.
This will rotate this object forever.
When trying to make it rotate on keypress i do the follows:

void keyboard_s (int key, int x, int y)
   switch (key)
   case GLUT_KEY_LEFT:

But i couldnt figure out how to tell it that i wanted it to do it to the rectangle. Since i have no point of reference.

Also i want to rotate this rectangle around a circle.
Do i use posX,posy,posz of the circle in some way to calculate how to do this.
I read that it's best to :
Make sure the shape is "facing" the fixed point(other shape?), so that translating forward with respect to the shape puts you closer to the center of its orbit
glTranslatef the shape forward to the point around which you want it to rotate
glRotatef the direction you want the spape to orbit
glTranslatef backwards just as far as you went forward

But i couldnt understand it.

Any advice and suggestions anyone could give would be much appreciated.

Thanks in advance Matt.

7 Years
Discussion Span
Last Post by thehivetyrant

My suggestion is to not use OpenGL directly, but rather, use VTK! (http://vtk.org/)

I have been working hard for the last year writing examples, which you can find here: http://www.vtk.org/Wiki/VTK/Examples

There is certainly a learning curve (but there is with OpenGL, too, as you're experiencing), but it's much cleaner than OpenGL once you get the hang of it.

Good luck,



>>i have this rectangle and i want to be able to rotate this shape around another when i press a key.

By default the rectangle rotates around the origin. So if you wan't the
rectangle to rotate around an object, then translate the world x units until the object to be rotated around is at the origin. Then rotate the rectangle. So the rectangle rotates around the origin, which is where the object now resides. After the rotation is done. Translates the world back -x units. This causes the appearance of rotation around shape.


Hey guys,

Hopefully your still around.

I've made so it rotates around the area i want very nicely and also that it rotates left/right when i press the left/right keys respectively.

But i have encountered a problem perhaps you can help me absolve.

When i switch from left to right it doesnt just change direction it seems to re-apply the translate or something because appears over the other side (mirrored position) and starts from there; effectively teleporting before continuing opposite rotation.

I hope that was clear.
Heres the code i have at present for it.

void rectangle (void) {

	if (Rotateleft == TRUE) // checks if left is pressed.
	glRotated((GLdouble)spin, batX,batY,batZ+=1.0);
	if (Rotateright == TRUE) // checks if right is pressed.
	glRotated((GLdouble)spin, batX,batY,batZ-=1.0);
                ... // creation of rectangle

So it seems i basically just need to reverse the direction it's rotating. But i can't figure it out.
Maybe i need to do translations somewhere else, (i have tried sticking outside the 'if's but it doesnt work.

Thanks in advance again.

Edited by thehivetyrant: n/a


>> So it seems i basically just need to reverse the direction it's rotating.

Try reversing the angle its rotating about. Also make it an else if statement.


I had posted another question but nevermind, I solved it.

Thanks for the help anyway, it was appreciated.

Good rep all 'round lads!

Edited by thehivetyrant: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.