Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

OpenGL [noob=ON] Rotating around a point

This topic is 4840 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);

    glPushMatrix();

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

    glBegin(GL_QUADS);
    
    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);            
    
    glEnd();
    
    glPopMatrix();

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
Advertisement
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

Translate(quad.position);
Rotate(quad.angle);
Translate(-quad.position);


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:




Identity();
Translate(quad.position);
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);

End();


Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!