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.

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);

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 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 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 originBegin(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 on other sites
Yup. I actually came up with this solution at my piano lesson. Funny how that works, eh? Thanks for your responses, though. ++rate.

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 12
• 15
• 12
• 14
• 9
• Forum Statistics

• Total Topics
633354
• Total Posts
3011495
• Who's Online (See full list)

There are no registered users currently online

×