Rotate Rectangle

I hate to ask this because it must have been asked a trillion times but I didn’t know how to search for it so here it goes

How can I rotate square tiles by 90 degrees?
I can reassign the vertex coordinates but that just seems dirty and there must be a better way
Note that it only be 90/180/270 degrees, nothing like 45

Thanks

I probably can’t answer this, but I can ask a few leading questions that might get you closer to your answer.

  1. What are you using to do your graphics? (Java2D, LWJGL, JOGL, Slick2D, LibGDX, etc.)
  2. Are you asking, specifically, about rotating the tile image for a single tile (Meaning using the unrotated image for other tiles)?

Good point.
I’m using the standard LWJGL/Slick Util set up
And I mean rotating a single tile, not the rest

One way to do this would be to change the order you lay out your texcoords when rendering your sprite. Another simple solution if you are using fixed-function would be to do the following:

float angle = 90;
//setup transform
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(angleDegrees, 0f, 0f, 1f);

.. draw sprite at (0, 0)..
   glBegin etc....

//reset transform
GL11.glRotatef(-angleDegrees, 0f, 0f, 1f);
GL11.glTranslatef(-x, -y, 0);

Oh… That seems to be exactly what I was looking for. Thank you!

Sorry I have to put my nose inside a topic again… But here is how you usually should do it:


float angle = 90;
//save current transform and start another one
GL11.glPopMatrix();
//setup transform
GL11.glTranslatef(x, y, 0);
GL11.glRotatef(angleDegrees, 0f, 0f, 1f);

.. draw sprite at (0, 0)..
   glBegin etc....

//reset transform
GL11.glPopMatrix();

Is there a performance advantage to doing that?
(And don’t worry about re answering the question. Shows you care!)

glPushMatrix saves the current state of the opengl matrix and glPopMatrix restores the state. So to prevent your rotation from affecting the rest of the opengl code you should put a call to glPushMatrix before your rotation and a call to glPopMatrix after the rotation.

Yup. I think so. Because rotating the matrix needs to do some sin() and cos() calls, which could take some time. Where glPushMatrix() and glPopMatrix() only have to do with memory allocation and things like that :slight_smile:

Why did you do glPopMatrix twice and no glPushMatrix at all?

| Nathan

I would guess that it was a typo, rather than a purposeful double-pop. Given what he said in his other post, it sounded like he intended it to be:


GL11.glPushMatrix();
//... Other stuff
GL11.glPopMatrix();

Yes. Sorry for that typo :slight_smile: