Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: rotating object

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    27

    rotating object

    I am trying to rotate a space ship like in asteroids here is the code I am working on.
    Code :
    void DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
    {
    	glPushMatrix();
    	glTranslatef(position_X, position_Y, position_Z);
    	glRotatef(rotation,0.0f,0.0f,1.0f);
    	glColor3f(color,0.0f,0.0f);
    	glBegin(GL_LINE_LOOP);
    	glVertex3f(0.0f,-0.25f,0.0f);
    	glVertex3f(-0.25f,-0.5f,0.0f);
    	glVertex3f(-0.5f,-0.5f,0.0f);
    	glVertex3f(-0.0f,0.5f,0.0f);
    	glVertex3f(0.5f,-0.5f,0.0f);
    	glVertex3f(0.25f,-0.5f,0.0f);
    	glVertex3f(0.0f,-0.25f,0.0f);
    	glEnd();
    	glPopMatrix();
    }
    I am able to get the ship to move up and down but I want it to follow its nose after rotation.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,106
    You should rotate before you translate.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2013
    Posts
    25
    Phil, I recognize that ship...funny. I have the current and full code listing that you posted on gameDev, I'll be taking a look at it right away. I'm pretty sure what I've already posted will do the trick but it still has to be put into the appropriate place. There is now a third page on that thread. I've made a slight change in how color is being handled.

    Quote Originally Posted by tonyo_au View Post
    You should rotate before you translate.
    Doing that will offset the ship from it's pivot point, when you rotate it, the ship will orbit as if it's on a swing arm.
    Last edited by marcClintDion; 07-07-2013 at 11:00 PM. Reason: Added an update

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by pbivens View Post
    I am able to get the ship to move up and down but I want it to follow its nose after rotation.
    If you want to move the ship (or fire bullets) in the direction it's facing, you need your own polar-to-cartesian conversion:
    Code :
    x = magnitude * cos(angle)
    y = magnitude * sin(angle)
    This assumes that angle is counter-clockwise with zero at the positive X axis. Bear in mind that the trigonometry functions in the standard C library use radians while glRotate() uses degrees.

    While it's possible to abuse OpenGL functions to do this for you (using glRotate() and glGet(GL_MODELVIEW_MATRIX)), doing it yourself is trivial and far more efficient (glGet() is expensive).

    FWIW, the inverse conversion is:
    Code :
    magnitude = sqrt(x*x+y*y)
    angle = atan2(y, x)
    Conversion between polar and cartesian coordinates is probably the only trigonometry you'll ever need for graphics; everything else is normally done using vectors.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    27
    cool thanks marc again.

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    27
    well I have tried several things but I am still stuck. Here is the code I am working on.
    Code :
    void DrawShip(GLfloat position_X,GLfloat position_Y, GLfloat position_Z, GLfloat rotation,GLfloat color)
    {
    	glPushMatrix();
    	glRotatef(rotation,0.0f,0.0f,1.0f);
    	glTranslatef(position_X, position_Y, position_Z);
    	glColor3f(color,0.0f,0.0f);
    	glBegin(GL_LINE_LOOP);
    	glVertex3f(0.0f*cos(angle)+0.25f*sin(angle),0.0f*sin(angle)-0.25f*cos(angle),0.0f);
    	glVertex3f(-0.25f*cos(angle)+0.5f*sin(angle),-0.25f*sin(angle)-0.5f*cos(angle),0.0f);
    	glVertex3f(-0.5f*cos(angle)+0.5f*sin(angle),-0.5f*sin(angle)-0.5f*cos(angle),0.0f);
    	glVertex3f(0.0f*cos(angle)-0.5f*sin(angle),0.0f*sin(angle)+0.5f*cos(angle),0.0f);
    	glVertex3f(0.5f*cos(angle)+0.5f*sin(angle),0.5f*sin(angle)-0.5f*cos(angle),0.0f);
    	glVertex3f(0.25f*cos(angle)+0.5f*sin(angle),0.25f*sin(angle)-0.5f*cos(angle),0.0f);
    	glVertex3f(0.0f*cos(angle)+0.25f*sin(angle),0.0f*sin(angle)-0.25f*cos(angle),0.0f);
    	glEnd();
    	glPopMatrix();
    }

  7. #7
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by pbivens View Post
    well I have tried several things but I am still stuck. Here is the code I am working on.
    There was nothing wrong with your original code. You just need to set position_X and position_Y correctly.

    For an asteroids clone, the update function should look something like:
    Code :
    double angle = (90 - rotation) * M_PI / 180;
    speed_X += acceleration * cos(angle);
    speed_Y += acceleration * sin(angle);
    position_X += speed_X;
    position_Y += speed_Y;

  8. #8
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    27
    Quote Originally Posted by GClements View Post
    If you want to move the ship (or fire bullets) in the direction it's facing, you need your own polar-to-cartesian conversion:
    Code :
    x = magnitude * cos(angle)
    y = magnitude * sin(angle)
    This assumes that angle is counter-clockwise with zero at the positive X axis. Bear in mind that the trigonometry functions in the standard C library use radians while glRotate() uses degrees.

    While it's possible to abuse OpenGL functions to do this for you (using glRotate() and glGet(GL_MODELVIEW_MATRIX)), doing it yourself is trivial and far more efficient (glGet() is expensive).

    FWIW, the inverse conversion is:
    Code :
    magnitude = sqrt(x*x+y*y)
    angle = atan2(y, x)
    Conversion between polar and cartesian coordinates is probably the only trigonometry you'll ever need for graphics; everything else is normally done using vectors.
    here is my new code
    Code :
    void update(int value)
    {
    	double magnitude = sqrt(ship_01_POSITION_X * ship_01_POSITION_X + ship_01_POSITION_Y * ship_01_POSITION_Y);
    	double angle = atan2(ship_01_POSITION_Y,ship_01_POSITION_X);
    	ship_01_POSITION_X = magnitude * cos(angle);
    	ship_01_POSITION_Y = magnitude * sin(angle);
    	glutPostRedisplay();
    	glutTimerFunc(25,update,0);
    }
    it moves up and down but does not rotate.

  9. #9
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    474
    Quote Originally Posted by pbivens View Post
    Code :
    	double angle = atan2(ship_01_POSITION_Y,ship_01_POSITION_X);
    This is wrong. The angle should be the direction the ship is facing.

  10. #10
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    27
    so how do I adjust the angle variable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •