Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Trouble Rotating

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    4

    Trouble Rotating

    I have a program that displays a 2D tanklike-object and has it move like a tank. The one problem I have is getting it to rotate like a tank. Every time it is rotated, it rotates, along the center of the screen, I want it to ratate along the center of the tank. Is there any thing I can do to fix this? the code for the program is below.

    Code :
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <GL/glut.h>
     
    float zRotationAngle = 0.0f;
     
    //Shared variables
    static float sbdiv = .9;
    static float sldiv = 1.1;
     
    //Main Square Coordinates
    static float bbottom = -.9;
    static float brx = .6;
    static float blx = -.6;
     
    //*
    //Smaller attatchment Coordinates
    //*
    static float srx = .2;
    static float slx = -.2;
     
    //*
    //Even smaller attatchment
    //*
    static float ltop = 1.8;
    static float lrx = .05;
    static float llx = -.05;
     
    void mydisplay(void)
    {
        glRotatef(zRotationAngle, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
     
     
        glBegin(GL_POLYGON);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(brx, sbdiv);
        glColor3f(0.0, 0.7, 0.0);
        glVertex2f(brx, bbottom);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(blx, bbottom);
        glColor3f(0.0, 0.7, 0.0);
        glVertex2f(blx, sbdiv);
        glEnd();
     
        glBegin(GL_POLYGON);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(srx, sldiv);
        glColor3f(0.0, 0.7, 0.0);
        glVertex2f(srx, sbdiv);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(slx, sbdiv);
        glColor3f(0.0, 0.7, 0.0);
        glVertex2f(slx, sldiv);
        glEnd();
     
        glBegin(GL_POLYGON);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(lrx, ltop);
        glColor3f(0.0, 0.7, 0.0);
        glVertex2f(lrx, sldiv);
        glColor3f(0.0, 0.0, 0.0);
        glVertex2f(llx, sldiv);
        glColor3f(0.0, 0.7, 0.0);
        glVertex2f(llx, ltop);
        glEnd();
     
        zRotationAngle = 0;
        glutSwapBuffers();
    }
     
    void spinDisplay(void)
    {
        glutPostRedisplay();
    }
     
    void myinit(void)
    {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glColor3f(1.0, 1.0, 1.0);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(-4.0, 4.0, -4.0, 4.0);
    }
     
    static void key(unsigned char key, int x, int y)
    {
        switch(key){
        case'w':
            sbdiv += .05;
            bbottom += .05;
            sldiv += .05;
            ltop +=.05;
            break;
        case 's':
            sbdiv -= .05;
            bbottom -= .05;
            sldiv -= .05;
            ltop -= .05;
            break;
        case'a':
            zRotationAngle += .5;
            break;
        case'd':
            zRotationAngle -= .5;
            break;
        case 27:
        case 'q':
            exit(0);
            break;
        }
        glutPostRedisplay();
    }
     
    int main(int argc, char **argv)
    {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowPosition(310, 0);
        glutCreateWindow("Moving Square");
     
        myinit();
        glutDisplayFunc(mydisplay);
        glutIdleFunc(spinDisplay);
        glutKeyboardFunc(key);
        glClearColor(1,1,1,1);
        glutMainLoop();
    }
    Last edited by Trek800; 06-17-2012 at 02:09 PM.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Location
    Poland
    Posts
    109
    Translate your object using matrices (glTranslate or custom uniform matrix passed to vertex shader)
    instead of modifying coordinates directly.

    In order to work as You expect program must do the object transformations in proper order:
    rotation and then translation. If You modify coordinates directly, then use glRotate and draw,
    rotation is performed last (translation is already pre-applied in coordinates).

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2012
    Posts
    4
    @kowal
    Thank you, I adjusted it to translate using matrices and it works perfectly.

Posting Permissions

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