I am simulating projectile motion in OpenGL. the problem that I am having is that I want to insert a delay between the plotting of points so that I can see the projectile moving from one point to another. I have inserted a delay using a sleep function of my own. But the problem that I am getting in this sleep function is that the output window is shown to me when all the points get plotted, it only shows me the final output. I am writing the codes of sleep() and point plotting piece so that if anyone here can help me in simulating the projectile motion by inserting some delays in between two points.

//Code for plotting points of projectile motion. This code is in the display() routine.

    vo = 60;
theta = 70;

thetar = (pi/180) * theta;

vox = vo * cos(thetar);
voy = vo * sin(thetar);

t = (0.0 - voy)/(-9.8);

dx = vox * t;
dy = voy * t + (0.5)*(-9.8)*(t * t);

total_t = 2 * t;

for(i = 0; i <= total_t; i = i + 0.1)
	dx = vox * i;
	dy = voy * i + (0.5)*(-9.8)*(i * i);

	glColor3f (0.0, 1.0, 0.0); //(red, green, blue)


	glBegin(GL_POINTS); // Center is at the mid of main window due to glOrtho()
		glVertex2f(dx, dy);

//Sleep() Routine

void sleep()
int i, j;

for(i = 0; i < 1000; i++)
	for(j = 0; j < 1000; j++);


There is no error in the above code. The only thing I want is the delay between plotting of two points of the projectile. If anyone can help me.

Well well.
The ugly simple way would be to draw to the front buffer, and do glFlush() after each point to be sure.

Call this once, before rendering :

The real solution is to split your physics code and rendering code.
Currently you have :

  • loop
    ++ compute one point
    ++ sleep
    ++ draw one point

swapbuffers somewhere.

So you only see the end result.

You should go for :


  • compute one point
  • draw one point

To see each intermediary result.

And btw, your sleep is bad :slight_smile: