# How can I rotate a quad around it's center?

My code is:

GL11.glPushMatrix();

// rotate around center
GL11.glTranslatef(-200/2, -200/2, 0);
GL11.glRotatef(30, 0.0f, 0.0f, 1);
GL11.glTranslatef(200/2, 200/2, 0);
// draw quad
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0f,0.5f);
GL11.glVertex3f(50,50,1);
GL11.glTexCoord2f(0.5f,0.5f);
GL11.glVertex3f(50+200,50,1);
GL11.glTexCoord2f(0.5f,0f);
GL11.glVertex3f(50+200,50+200,1);
GL11.glTexCoord2f(0f,0f);
GL11.glVertex3f(50,50+200,1);
GL11.glEnd();

GL11.glPopMatrix();


and it's not rotating it around the center, am I doing this correctly? if not how would I?

• You know you can edit your question instead of deleting/recreating, right? – Vaillancourt Jun 18 '15 at 23:17
• Yeah, but I didn't think I could change the title when editing it. – user2535688 Jun 18 '15 at 23:18

## 1 Answer

Your problem is your translation matrices. You are not translating enough.

Your quad starts at position (50, 50) and is of size (200, 200). That means we have 4 vertices:

( 50,  50)
(250,  50)
(250, 250)
( 50, 250)


You are applying a translation of 200/2 (100) on both axis, which will transform your vertices to these:

(-50, -50)
(150, -50)
(150, 150)
(-50, 150)


As you can see, these vertices are not centered. There is something that is missing: you need to translate according to your initial position (50, 50) too. So the translation you need to make would not be 200/2, but 200/2 + 50, which equals 150 and transforms the vertices like so:

(-100, -100)
( 100, -100)
( 100,  100)
(-100,  100)


Now we're talking! That seems way better! But don't forget to apply the opposite translation for the quad to come back to it's position. It should give this code:

// rotate around center
GL11.glTranslatef(-(200/2 + 50), -(200/2 + 50), 0); // Or put -150 for X and Y
GL11.glRotatef(30, 0.0f, 0.0f, 1);
GL11.glTranslatef(200/2 + 50, 200/2 + 50, 0); // Or put 150 for X and Y