0

I am drawing a cube which I want to move on its own 3 axis (not the ones from the base coordinates). The cube will never be located on the origin, this is why i need to use the glTranslatef() function.

I don't understand:

When I rotate the cube on its y-axis everything goes fine. When I try to rotate it on its x-axis it doesn't work properly: it starts moving accros my screen and doesn't stay on its position.

works fine: (my cube rotates on its own axis while staying in its own position(0,0.5,0))

glTranslatef(0, 0, 0);
glRotatef(getCubeAngle(), 0.0f, 1.0f, 0.0f); 
glTranslatef( 0.5, 0, 0.0); 

doesn't work fine: (cube rotates on its own axis + the cube starts moving over my screen along the x-axis)

   glTranslatef(0, 0, 0);
   glRotatef(getCubeAngle(), 0.0f, 1.0f, 0.0f); 
   glTranslatef( 0.5, 0, 0.0);
/*glTranslatef( 0.0, 0.5, 0.0); with this line instead it works well*/

why?

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • Check my [answer](http://stackoverflow.com/questions/26743207/rotating-around-a-point-different-from-origin/26743386#26743386) about rotating object around different point rather than the origin – Berke Cagkan Toptas Apr 18 '15 at 17:33

1 Answers1

0

Rotation matrices will always use the (current) origin as center for the rotation. When you want to rotate around a different point, you need to translate first, so that the pivot point becomes the local origin.

I don't know what coordinates your cube actually have in object space, So I don't know where exactly the center is. But from you description one can derive that the center is not at (-0.5, 0, 0), which you currenlty use as rotation center. Keep in mind that the order of translatations is inverted to what you probably expect. Your code first translates by (0.5,0,0) and then rotates.

My guess would be that your cube is actually already centered at the origin, and you just got the order of transformations backwards.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • thank you for your answer. Well I guess the center is 0,0,0 like always, right? Inverting both translations worked out fine. Thx! – the1liquid Apr 18 '15 at 14:16
  • The center is whatever whoever defined the geomerty for your cube chose as the center. – derhass Apr 18 '15 at 14:33