#include <stdlib.h>
#include <stdio.h>
#include <gl/glut.h>
#include <math.h>
#include <sys\timeb.h>

/*globals*/

double tint=5.0;
float xi=-2.0;
float yi=2.0;
float vxi=.5;
float vyi=0.0;
float vy, vx;
float accel=-9;
float damp=0.75;
/***********************************************************/
void display()
{

/*locals*/
struct timeb a;
struct timeb c;
double tchange;
float pos[2];
float vv[]={-2,-1.5};
float xx[]={2,-1.5};

/*time initializer and changer*/
if ((tint==5.0))
{
	ftime(& a);
	tint=a.time+a.millitm/1000.0;
	tchange=tint;
}
else
{
	ftime(& c);
	tchange=c.time+c.millitm/1000.0;
}

/*kinematics for motion*/
pos[0]=xi;
pos[1]=yi;
pos[0]=pos[0]+(vxi*(tchange-tint));
pos[1]=pos[1]+(vyi*(tchange-tint)+accel*(tchange-tint)*(tchange-tint)/2.0);

/*collision and kinematics for bounce*/
if(pos[1]<-1.5)
{
vy=vyi+accel*(tchange-tint);
vyi=-damp*vy;
tint=5.0;
xi=pos[0];
yi=-1.5;
}

/*gl of line for collision and path*/
glBegin(GL_POINTS);
glColor3f(0.0,1.0,0.0);
glVertex2fv(pos);
glEnd();
glBegin(GL_LINES);
glColor3f(0.0,0.0,1.0);
glVertex2fv(vv);
glVertex2fv(xx);
glEnd();
}
/***********************************************************/
void keyboard(unsigned char key, int x, int y){
switch(key){
case 27: exit(0); break; /* escape with the (ESC) key */
case 'w': glClear(GL_COLOR_BUFFER_BIT); break; /* (W)ipe screen */
}
}
/***********************************************************/
void idle(void){ glutPostRedisplay(); }
/***********************************************************/
int main(int argc, char **argv){ /* pure GLUTtery here */
glutInitWindowSize(800, 800);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("<< Bounce in GLUT >>");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0,2.0,-2.0,2.0,-2.0,2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutMainLoop();
return 0;}