How to update frame to move a 3d object?

I have a 3d object I want to move continuously, I wrote this piece of code ( in the display function) but the frames appear connected to each other like a flash, how do I fix that? should I use a delay function ( because I tried and the window froze completely) .

if (moveRight==1)
{

	for (float v = 0; v < 100; v++)
	{
		x += v;
		
		update(x, -44, -7);
		
		
	}
}

the update function is to move the ball one step only and the (moveRight) is for the keyboard, when I press the right arrow key I want the ball to move on its own.

also I’ve been using openGL for months but I’m still just a beginner, so I would appreciate if you keep it simple for me, and thank you in advance.

The simplest solution is to do one update each frame. However, if the frame rate varies, that will result in the animation speed varying with the frame rate. There are two common approaches to deal with that:

  1. Scale the update according to the elapsed time since the previous update, e.g.
const int float = 0.05;  // speed is units per millisecond.
static int last;
int now = glutGet(GLUT_ELAPSED_TIME);
if (!last)
    last = now;
int delta = now -last; // milliseconds
last = now;
...
x += delta * speed;
update(x);
  1. Use a fixed timestep but call the update function a variable number of times. E.g.
const int interval = 20; // milliseconds between updates
static int next; // time when next update is due
int now = glutGet(GLUT_ELAPSED_TIME);
if (!next)
    next = now;
while (next < now) {
    update();
    next += interval;
}

In practice, you may want to clamp the number of updates so that if the interval between updates is particularly long (e.g. the user locks their computer) the animation will pause rather than skipping ahead however many minutes have passed.

1 Like

A post was split to a new topic: Camera Navigation Not Working