1
\$\begingroup\$

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?

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

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.