Sign in to follow this  

2d Rotation Problem

This topic is 4136 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


Link to post
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.

Share this post


Link to post
Share on other sites

This topic is 4136 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this