Jump to content
• Advertisement

# 2d Rotation Problem

This topic is 4357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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 this post

##### Share on other sites
Advertisement
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.

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
• Advertisement

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631790
• Total Posts
3002365
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!