# OpenGL Rotation and Translation

This topic is 3645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Goal: I'm trying to have a square drawn with it's center at (square.X(), square.Y()), rotated by square.Orientation(). ( So no matter what it's orientation is, it's center will be at (x, y). ) Problem: I thought I would be able to do that by first rotating and then translating, it does rotate the square but it looks like it also rotates the direction in which it translates.. So then I thought I'd try first translating and then rotating, but that didn't do much good either.. Code: OpenGL initialization:
// Set up OpenGL
glMatrixMode( GL_PROJECTION );
glOrtho( 0.0, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0, -1.0, 1.0 );
glMatrixMode( GL_MODELVIEW );
glClearColor( 0.0, 0.0, 0.0, 0.0 );
if ( glGetError() != GL_NO_ERROR ) {
ShowMsg( "Could not set up OpenGL." );
return false;
}


Drawing square:
void Draw( Square& square )
{
glClear( GL_COLOR_BUFFER_BIT );

glTranslatef( square.X(), square.Y(), 0.0 );
glRotatef( square.Orientation(), 0.0, 0.0, 1.0 );
glColor4f( 1.0, 1.0, 1.0, 1.0 );
glVertex2f( square.Left(),  square.Top() );
glVertex2f( square.Right(), square.Top() );
glVertex2f( square.Right(), square.Bottom() );
glVertex2f( square.Left(),  square.Bottom() );
glEnd();

// Update screen
SDL_GL_SwapBuffers();
}


Question: Could anyone help me out with the goal I'm achieving and point out what I'm doing wrong at the moment? Thank you in advance, Arjan

##### Share on other sites
You'll have to draw the square with it's center at [0,0] (in local space), and do rotation first, then have a translation that transforms that center to the correct position.

##### Share on other sites

Quote:
 Original post by quasar3d(1) draw the square with it's center at [0,0] (in local space), (2) and do rotation first, (3) then have a translation that transforms that center to the correct position.

Isn't that what this does?
(1) glLoadIdentity();(3) glTranslatef( x, y, 0 );(2) glRotatef( angle, 0, 0, 1 );

Switching (2) and (3) didn't help either ^^.
By the way: before these calls, the matrix mode is GL_MODELVIEW ( in case that matters ).

##### Share on other sites
No, what it means is that the squares center must be located at the origin for the rotation to be as you describe. For example, if your square ranges from 0 to 1 along both axes, the center is at (0.5, 0.5) which is not the origin. For your square to be located at the center, square.top() == -square.bottom() and square.right() == -square.left() must be true.

##### Share on other sites
Ah, of course! ^^
That fixed it, thank you.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634050
• Total Posts
3015242
×