2d Rotation Problem

Recommended Posts

So I'm making an Asteroids type game and I got the math working correctly with taking the velocity and multiplying it by the sine and cosine of the angle. I call rotatef using the angle and then translate the ship to the location but, when I call glRotatef on the ship, it rotates about the origin (middle of the screen), instead of its local coordinates. My code looks like Render() { glLoadIdentity(); drawShip(ship.x, ship.y, ship.a); } //draw ship drawShip() { glPushMatrix(); //rotate the ship, then translate it to the appropriate position glRotatef(a - 90.0f, 0.0f, 0.0f, 1.0f); glTranslatef(x, y, -50.0f); //draw ship glBegin(GL_LINES); glColor3f(0.0, 1.0, 0.0); glVertex3f(0.0f,1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f(0.0f,1.0f, 0.0f); glVertex3f(1.0f,-1.0f, 0.0f); glVertex3f(1.0f,-1.0f, 0.0f); glVertex3f(0.0f,-0.5f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f(+0.0f,-0.5f, 0.0f); glEnd(); glPopMatrix(); } The starting angle (ship.a) I put into the ship is 90.0 so that it would work out where I could calculate the y value with sin. So I'm wondering what am I doing wrong. My calculations (which I have determined to be correct) for the angles look like: //ship.v is velocity, ship.a is angle ship.x += ship.v * cos(piover180*ship.a); ship.y += ship.v * sin(piover180*ship.a); Thanks John

Share on other sites
Most likely you just need to reverse the order of operations, i.e. this:
glRotatef(a - 90.0f, 0.0f, 0.0f, 1.0f);glTranslatef(x, y, -50.0f);
Becomes this:
glTranslatef(x, y, -50.0f);glRotatef(a - 90.0f, 0.0f, 0.0f, 1.0f);
There are different ways you can think about it, but ultimately what it comes down to is that with OpenGL, transforms are applied in the reverse of the order in which they're issued in your code.

Create an account

Register a new account

• Partner Spotlight

• Forum Statistics

• Total Topics
627636
• Total Posts
2978319

• 10
• 12
• 22
• 13
• 33