Unable to rotate a 2D triangular polygon via glRotatef().

Here’s a (big) GIF of me holding down the “TURN” key and “FORWARD” key together, and below is the code:


	public void draw() {
		GL11.glColor3f(0f, 1f, 1f);
		GL11.glBegin(GL11.GL_LINE_LOOP);
		{
			GL11.glPushMatrix();
			GL11.glTranslatef(-x, -y, 0f);
			GL11.glRotatef(angle, 0, 0, 1f);
			GL11.glVertex2f(x, (y - 10));
			GL11.glVertex2f((x + 8), (y + 10));
			GL11.glVertex2f(x, (y + 5));
			GL11.glVertex2f((x - 8), (y + 10));
			GL11.glVertex2f(x, (y - 10));
			GL11.glTranslatef(x, y, 0f);
			GL11.glPopMatrix();
		}
		GL11.glEnd();
		GL11.glColor3f(1f, 1f, 1f);
	}

I don’t know how to rotate the “ship” around the Z axis.

EDIT: Forgot to mention that I did tried many different combinations of “glTranslate()” and “glRotate()” in different spots in the draw() code. They either skew the ship’s vertices around, or they don’t do anything. And I’m getting all confused as to why.

  1. Why is there another glTranslate right before glPopMatrix? this won’t do anything cause glPopMatrix undos it.

  2. If you set The Position of the Ship with a glTranslate why is there x and y coordinates in the glVertex2f methods? doesn’t make sense to me

Try this:

public void draw() {
      GL11.glPushMatrix();
      GL11.glTranslatef(-x, -y, 0f);
      GL11.glRotatef(angle, 0, 0, 1f);
      GL11.glColor3f(0f, 1f, 1f);
      GL11.glBegin(GL11.GL_LINE_LOOP);
      {
         GL11.glVertex2f(0,  - 10);
         GL11.glVertex2f( 8, 10);
         GL11.glVertex2f(0, 5);
         GL11.glVertex2f( - 8, 10);
         GL11.glVertex2f(0, - 10);
      }
      GL11.glEnd();
      GL11.glPopMatrix();
   }

That’s how i would do it

  1. Hm… I guess I didn’t clean up my code after doing many different glTranslate() and glRotate() combos.

  2. I thought that glTranslatef() moves the rotation pivot point to (x, y), then glRotate3f() rotates the entire view around the pivot point, and then we do glTranslatef() to move the pivot point back to the origin.

Okay, it’s fixed. The glTranslatef() in your code has the X and Y coordinates inverted, so I didn’t see my ship at first. It was when I was playing around with it, do then realized my ship had been rendering at the wrong spot.

And then once the ship is showing up, the ship’s rotation became incorrect. Been working on that for quite a while, until it clicked that I was passing radians to glRotate3f(), when it actually needed inverted degrees. So, got that fixed, and now my ship is working.

Here’s the working code:


	public void draw() {
		GL11.glPushMatrix();
		GL11.glTranslatef(x, y, 0f);
		GL11.glRotatef((float) -(Math.toDegrees(angle)), 0f, 0f, 1f);
		GL11.glColor3f(0f, 1f, 1f);
		GL11.glBegin(GL11.GL_LINE_LOOP);
		{
			GL11.glVertex2i(0, -10);
			GL11.glVertex2i(8, 10);
			GL11.glVertex2i(0, 5);
			GL11.glVertex2i(-8, 10);
			GL11.glVertex2i(0, -10);
		}
		GL11.glEnd();
		GL11.glPopMatrix();
		GL11.glColor3f(1f, 1f, 1f);
	}

In the end, Seiya02 gave me a wonderful hint. Thank you!

Couldn’t know your angle was in radians haha :smiley:
I used inverted x and y coordinates cause you did it too in your example code :smiley:
Glad i could help you