Advertisement Jump to content
Sign in to follow this  

OpenGL [noob=ON] Rotating around a point

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a top-view 2D game using OpenGL/SDL. The only reason I'm using OpenGL for this is for the rotation. Um, I just need to know how to rotate a quad around it's center. I have lots of objects in the game, which all store the angle they are rotated at. During the render, I do this:
    glBindTexture(GL_TEXTURE_2D, tx->texture_id);


    glRotatef(angle, 0.0f, 0.0f, 1.0f);    

    glTexCoord2f(0.0f, 0.0f); glVertex3f(x * scale, y * scale, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f((x + tx->w) * scale, y * scale, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f((x + tx->w) * scale, (y + tx->h) * scale, 0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(x * scale, (y + tx->h) * scale, 0.0f);            

However, this rotates the object around point (0, 0) instead of around the center of the quad. I'm not sure how I should change the glRotatef() call to do this. Can you fill me in? Thanks.

Share this post

Link to post
Share on other sites
Translate the quad so that it's center is at the origin, do the rotation, and then translate it back (same translation but reversed).

Share this post

Link to post
Share on other sites

pseudo GL code


First you translate the quad in its center (this is the last line)(GL executes operation in the 'opposite order' due to matrix notation...)
Then rotate. The first line restore the quad in its original position.
Note that to be correct, 'position' must be the position of the center of the quad

You can also simplify the code.
Another variant can be something like this:

Scale(quad.scale/2); // because the centered quad has an edge len = 2!
Rotate(quad.angle, Z_AXIS); // you rotate around the origin

Begin(QUADS); // draw a centered quad

TexCoord (0, 0); Vertex (-1, -1);
TexCoord (1, 0); Vertex (+1, -1);
TexCoord (1, 1); Vertex (+1, +1);
TexCoord (0, 1); Vertex (-1, +1);


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!