Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Opengl glut Ball continuous animation

Options
  • 15-04-2006 7:33pm
    #1
    Registered Users Posts: 1,552 ✭✭✭


    Hi Im using opengl and glut.
    Ive gotten it to draw a sphere for a ball and move across the screen and back.
    I dont know how to get the ball to keep moving lleft and right across the screen forever.
    How would I do that?


Comments

  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    [edit] ok my previous answer wasn't very helpful, sorry about that.
    Anyway, just need a bit more info. How are you getting it to move across and back once? Using the idle function is a good way to do it.

    To declare the idle function put
    glutIdleFunc( YOUR_IDLE_FUNCTION_NAME_HERE );
    
    in where you call gutMainLoop(..);

    At the end of your main loop function put
    glutPostRedisplay();
    

    The idle function basically is for doing background processing, so all of the continuous animation calculations can be done there.

    Animations like this can be done in cycles, e.g. one cycle could start in the middle, move to the left, then move to the right, then back to the middle again. To do this you need variables to hold the cycle length (no. of frames per cycle) and the current frame.

    I'm not going to tell you the rest of how to do it, as you should be able to go from here. One tip though, the Sine function is an easy way to make animations look far smoother.


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    Thanks
    Im using translation to get the ball back and forth.
    This is the ball movement part of my code
    glPushMatrix();

    glTranslatef(across,0.1,0.5);
    glTranslatef(back,0.1,0.5);

    glutSolidSphere(0.05,36,10);

    if(across<0.4)
    {
    across =across+across;
    }
    if(back>-0.2)
    {

    back=back+back;

    }


    glPopMatrix();

    glutSwapBuffers();
    glutPostRedisplay();
    This is inside a drawImage method which I call in my main using
    glutDisplayFunc(drawImage);
    glutIdleFunc(drawImage);
    I tried using postReDisplay at the end of main and at the end of the drawImage method and it still only moved the ball back and forth once.
    Is there a way of just getting to do the same ball movement forever back and forth on its own?


  • Registered Users Posts: 5,112 ✭✭✭Blowfish


    Ah I see, sorry I wasn't very clear. The idle function is actually a seperate one to drawImage().

    If you declare two int global variables, one for the number of frames per cycle, and one for the current frame, initialise the first one (try 60 first, but you probably will have to change it up or down to get the speed reasonable), and the second one to zero.

    You actually don't need the if statements that you have. Instead you will work out the stuff in the idle function. Also at the end of every frame, just before glutPostRedisplay(), increment the current frame.

    Create the idle function. In it, the first thing you should do is to calculate where in the cycle the current frame is,
    if(currentFrame>cycleLength)
      currentFrame=0;
    
    float h  = (float)currentFrame/(float)cycleLength;
    

    This number is obviously going to stay between 0 and 1. The next thing to do is to work out the key points in your animation, and map them into the cycle. i.e. at h == 0.0 the ball should be in the middle. It then should go right until h == 0.25, then left until h == 0.75, then end up back in the middle at h == 1.0.

    (of course if you wanted to you could do the same thing as above in one line, you could, using the Sine function)

    Since the current frame will continuously reset when it gets too high, this will simply loop and keep repeating the cycle. Hope this helps :)


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    Brilliant thank you


Advertisement