Advertisement

rotate ship

Started by October 13, 2019 11:35 PM
4 comments, last by phil67rpg 4 years, 11 months ago

I have a very simple question, I am trying to rotate some vertex's around an arbitrary axis. basically I want to use glRotatef and glTranslatef to rotate a space ship I have drawn on the screen. here is my code of my  ship. what it does do is rotate around the origin when I  use the arrow keys left and right.


void drawShip()
{
	glPushMatrix();
	glColor3f(255.0f, 0.0f, 0.0f);
	glTranslatef(-50.0f, 0.0f, 0.0f);
	glRotatef(rotateship, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(50.0f, 0.0f, 0.0f);
	glVertex3f(45.0f, -5.0f, 0.0f);
	glVertex3f(50.0f, 10.0f, 0.0f);
	glVertex3f(55.0f, -5.0f, 0.0f);
	glEnd();
	glTranslatef(50.0f, 0.0f, 0.0f);
	glPopMatrix();
}

 

Phil, are you still using those old tools you learned in college many years ago before better tools were developed?

-- Tom Sloper -- sloperama.com

Advertisement
5 hours ago, phil67rpg said:

what it does do is rotate around the origin when I  use the arrow keys left and right.

It only looks like you are rotating about the origin because your ship is drawn uncentered.


glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-5.0f, -5.0f, 0.0f);
glVertex3f(0.0f, 10.0f, 0.0f);
glVertex3f(5.0f, -5.0f, 0.0f);

Try those instead.

Also you probably don't need to use glPushMatrix/glPopMatrix. Just glLoadIdentity at the beginning of each draw routine. My drawShip() would look like...


void drawShip()
{
   glLoadIdentity();
   glTranslatef(-50.0f, 0.0f, 0.0f);
   glRotatef(shipAngle, 0.0f, 0.0f, 1.0f);  //rotateship is not a good name for a variable

   glColor3f(255.0f, 0.0f, 0.0f);
   glBegin(GL_LINE_LOOP);
   {
      glVertex3f(0.0f, 0.0f, 0.0f);
      glVertex3f(-5.0f, -5.0f, 0.0f);
      glVertex3f(0.0f, 10.0f, 0.0f);
      glVertex3f(5.0f, -5.0f, 0.0f);
   }
   glEnd();
}

 

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

well fleabay I got it to work I just had to draw at the origin first and then translate it over to the new  position, I am trying to draw two space ships that rotate and shoot  bullets at each other.

you can close this thread.

This topic is closed to new replies.

Advertisement