Thread: Problem with translations and timers?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Problem with translations and timers?

    I've started OpenGL animation and I am trying to create a program that moves a dot up and down. It's meant to work by if functions, keeping track of when my variable for transformations (y axis) goes above or below 0.9 and -0.9 respectively.

    However, on running the applications I briefly see the dot just fly out the top of the window and I can't work out what's going wrong with it. Could anybody take a look and see if they know the problem? Thank you so much - here's the code:

    Code:
    #include <glut.h>
    
    void display(void);
    void timer(void);
    
    float amount = 0.91;
    
    int main(int argc , char **argv)
    {
        glutInit(&argc , argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowSize(600 , 300);
        glutInitWindowPosition(250 , 250);
        glutCreateWindow("moving dot");
        glutDisplayFunc(display);
        glutTimerFunc(500 , timer , 0);
    
        glClearColor(0.0 , 0.0 , 0.0 , 1.0);
        glColor3f(1.0 , 1.0 , 1.0);
    
        glutMainLoop();
    }
    
    void timer(void)
    {
        if(amount > 0.9)
        {
            amount = amount - 0.01;
        }
    
        else if(amount < -0.9)
        {
            amount = amount + 0.01;
        }
    
    
        glTranslatef(0.0 , amount , 0.0);
        glutPostRedisplay();
    }
    
    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glPointSize(5);
        glBegin(GL_POINTS);
        glVertex2f(0.0 , 0.0);
        glEnd();
    
        glutSwapBuffers();
    
        glFlush();
        glutTimerFunc(500 , timer , 0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If amount is, say, 0.9 it will never move (it is neither above 0.9, nor below -0.9), so every glTranslate will move the dot by 0.9 in the y direction, forever and ever.

Popular pages Recent additions subscribe to a feed