Part of the Khronos Group

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Need help with rotating a car

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2012

    Need help with rotating a car

    I'm new to opengl and studying it for one of my modules and for one of our assignments we have to make a car move through a maze and reach the top

    The thing is my car starts facing a horizontal position but when i click up I try to rotate the car so it faces the right direction or up but even when I put the glRotate(0,0,0,0) code it doesn't work... Instead it moves up and when I press right it goes diagonal between up and right and it doesn't go down

    I used the glRotate(90,0,0,0) code but all it never rotated the car up

    what do the four 0s stand for, one degree and x y z???

    here is half of my code

    I made a function for my car drawcar... do I have to put the rotate code in display or in my movement special keys code?

    void SpecialKeys(int key, int x, int y) // a function for the movement key - car moves

    if(key == GLUT_KEY_UP)


    drawCary = drawCary + carSpeed;

    if(key == GLUT_KEY_DOWN)
    drawCary = drawCary - carSpeed;

    if(key == GLUT_KEY_LEFT)
    drawCarx = drawCarx - carSpeed;

    if(key == GLUT_KEY_RIGHT)
    drawCarx = drawCarx + carSpeed;



    /* display callback function
    called whenever contents of window need to be re-displayed */
    //this is the all important drawing method - all drawing code goes in here
    void display(void)

    glClear (GL_COLOR_BUFFER_BIT); /* clear window */

    glLoadIdentity(); // so it doesn't multiply
    glTranslatef(drawCarx-15,drawCary-15,0); //

    drawCar(6,0,0); //displays the car function

    glFlush(); /* execute drawing commands in buffer */

    glLoadIdentity(); // so it doesn't multiply

    drawBorder(6,0,0); //displays the car function

    glFlush(); /* execute drawing commands in buffer */


    /* graphics initialisation */
    void init(void)
    glClearColor (0.0, 0.0, 0.0, 0.0); /* window will be cleared to black */

    //rename this to main(...) and change example 2 to run this main function
    int main(int argc, char** argv)
    /* window management code ... */
    /* initialises GLUT and processes any command line arguments */

    glutInit(&argc, argv);
    /* use single-buffered window and RGBA colour model */
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    /* window width = 400 pixels, height = 400 pixels */
    glutInitWindowSize (600, 600);
    /* window upper left corner at (100, 100) */
    glutInitWindowPosition (100, 100);
    /* creates an OpenGL window with command argument in its title bar */
    glutCreateWindow ("Example 1");



    return 0;

    Read more:

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    glRotate requires one of the axis x,y,z to be set eg glRotate(90,0,1,0) rotate 90 degrees around the y axis.

    These questions are better posted in the "beginners" forum because they can be answered by anyone using OpenGL not just MS Windows users

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2012
    thank you it works now but when it rotates i kind of moves a bit but it's better than nothing I guess

  4. #4
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Los Angeles
    Quote Originally Posted by Tezelia View Post
    thank you it works now but when it rotates i kind of moves a bit but it's better than nothing I guess
    This is probably because you aren't rotating around the geometric center of the car, which is probably because the car is not modeled around its geometric center. Put a glTranslate command in your draw_car routine somewhere between the Rotate command and the commands which draw the car. Try different values for X and Y until the car rotates around the point you want it to.

Posting Permissions

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