problem in animation

hey everybody for now I have been using glutIdleFunc() for animating my objects


void update()
{
   if(clock()- storeTime> 20)//storeTime has record of time
   {
      storeTime= clock();
      doTheAnimation();
      glutPostRedisplay();
   }
}

I used single frame display for animation.
Now with above tools I am not able to get a fairly smooth animation. Any better techniques?

Well what is the resolution of your clock ?
Even if it returns milliseconds, it can still advance only 10 times per second. Try to verify that by printing continuously values returned by clock().

Even if the clock() is fine, it can be better to render in a loop, animating according to elapsed time since previous frame, but with vsync on so that you do not render more frames than actually needed.

resolution of clock is milliseconds. How to use vsync?

The time taken for calculations between two frames is such that it is not sufficient to get more than 50 fps. Now for animation of a ball, I am displacing the ball after each frame by a certain amount. Let’s say I want to increase the speed of ball, by keeping the fps unchanged. What am i supposed to do? Increase the displacement vector, I guess. Now if the calculations are dependent on the ball position and due to increase in displacement vector, the position is changing at big intervals than previous one and so the calculations are getting disturbed and missing some events which are not to be missed.
How to solve it?

Calculate N sub steps, render only the last.

how will it help me increase the ball velocity?

Imagine you split doTheAnimation() in two :

  • computeTheAnimation
  • drawTheAnimation
    Now you can do this :

void update()
{
   if(clock()- storeTime> 20)//storeTime has record of time
   {
      storeTime= clock();
      computeTheAnimation();
      computeTheAnimation();
      drawTheAnimation();

      glutPostRedisplay();
   }
}

This will appear twice faster.
Or do the right thing, make your dynamics engine completely independant of rendering speed, either by running it at fixed time intervals, or by making it more robust to accept any time interval.

I thought my calculations were consuming time but the fact it, the drawing part was consuming time and by doing the calculations twice, things are going fine. :smiley:
Thanx

Now for making it completely independent of rendering speed, I was thinking to do that but since there were more objects in my game, I thought it wrong practice to keep one variable for storing time for all such objects. But now I understand the need for it. Thanx once again.

I have a doubt. Let’s say I keep the dyanmics engine independent of rendering speed. How will I synchronize it with the rendering speed?

For now what I know about the animation is glutIdleFunc() and the only place where I can ask ball to move and perform calculations is in this function only. How both the things can be handled independently?

If you’re seriously making a “dynamics engine,” (though to be honest, I’d just download Bullet Physics and be done with it) then it’s time to abandon GLUT.

GLUT is a nice tool for what it is. It’s useful for making small, simple applications. But once you start down the path of making something serious, you need to use better tools. Libraries like SDL or GLFW are better at creating real applications.

It’s mostly a matter of timing. GLUT makes no guarantees about when the idle function is called, how often it’s called, etc. Using other tools will allow you to make the guarantees that you will need in order to make your physics engine work.

Thanx Alfonse Reinheart

I will have a look at these tools.