#include <stdlib.h>
#include <stdio.h>
#include <gl/glut.h>
#include <math.h>
#include <sys\timeb.h>

/*globals*/

double tint=5.0;
float xi=3.0;
float yi=9.0;
float vxi=-6.0;
float vyi=0.0;
float vy, vx;
float yaccel=-6;
float xaccel=0;
float dampy=1;
float dampx=1;

double tint2=5.0;
float xi2=3.0;
float yi2=-4.0;
float vxi2=7.0;
float vyi2=7.0;
float vy2, vx2;
float yaccel2=-6;
float xaccel2=0;
float dampy2=1;
float dampx2=1;
float temp;
float coldelay=10.0;
/***********************************************************/
void display()
{
/*locals*/
struct timeb a;
struct timeb c;
double tchange;
float pos[2];

struct timeb a2;
struct timeb c2;
double tchange2;
float pos2[2];



float vv[]={1,1};
float xx[]={9,1};
float jj[]={1,10};
float hh[]={9,10};
/*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;
}

if ((tint2==5.0))
{
	ftime(& a2);
	tint2=a2.time+a2.millitm/1000.0;
	tchange2=tint2;
}
else
{
	ftime(& c2);
	tchange2=c2.time+c2.millitm/1000.0;
}


/*kinematics for motion*/
pos[0]=xi;
pos[1]=yi;
pos[0]=pos[0]+(vxi*(tchange-tint)+xaccel*(tchange-tint)*(tchange-tint)/2.0);
pos[1]=pos[1]+(vyi*(tchange-tint)+yaccel*(tchange-tint)*(tchange-tint)/2.0);


pos2[0]=xi2;
pos2[1]=yi2;
pos2[0]=pos2[0]+(vxi2*(tchange2-tint2)+xaccel2*(tchange2-tint2)*(tchange2-tint2)/2.0);
pos2[1]=pos2[1]+(vyi2*(tchange2-tint2)+yaccel2*(tchange2-tint2)*(tchange2-tint2)/2.0);


/*collision and kinematics for bounce*/
if(pos[1]<1.0)
{
vy=vyi+yaccel*(tchange-tint);
vyi=-dampy*vy;
vx=vxi+xaccel*(tchange-tint);
vxi=vx;
tint=5.0;
xi=pos[0];
yi=1.0;
}

if(pos2[1]<1.0)
{
vy2=vyi2+yaccel2*(tchange2-tint2);
vyi2=-dampy2*vy2;
vx2=vxi2+xaccel2*(tchange2-tint2);
vxi2=vx2;
tint2=5.0;
xi2=pos2[0];
yi2=1.0;
}

if(pos[0]<1.0)
{
vy=vyi+yaccel*(tchange-tint);
vyi=vy;
vx=vxi+xaccel*(tchange-tint);
vxi=-dampx*vx;
tint=5.0;
xi=1.0;
yi=pos[1];
}

if(pos2[0]<1.0)
{
vy2=vyi2+yaccel2*(tchange2-tint2);
vyi2=vy2;
vx2=vxi2+xaccel2*(tchange2-tint2);
vxi2=-dampx2*vx2;
tint2=5.0;
xi2=1.0;
yi2=pos2[1];
}

if(pos[0]>9.0)
{
vy=vyi+yaccel*(tchange-tint);
vyi=vy;
vx=vxi+xaccel*(tchange-tint);
vxi=-dampx*vx;
tint=5.0;
xi=9.0;
yi=pos[1];
}

if(pos2[0]>9.0)
{
vy2=vyi2+yaccel2*(tchange2-tint2);
vyi2=vy2;
vx2=vxi2+xaccel2*(tchange2-tint2);
vxi2=-dampx2*vx2;
tint2=5.0;
xi2=9.0;
yi2=pos2[1];
}

if((fabs(pos[0]-pos2[0])<.3) && (fabs(pos[1]-pos2[1])<.3) && coldelay>=10.0)
{
vy2=vyi2+yaccel2*(tchange2-tint2);
vyi2=vy2;
vy=vyi+yaccel*(tchange-tint);
vyi=vy;
vx=vxi+xaccel*(tchange-tint);
vxi=vx;
vx2=vxi2+xaccel2*(tchange2-tint2);
vxi2=vx2;



temp=vyi2;
vyi2=dampy*vyi;
vyi=dampy*temp;

temp=vxi2;
vxi2=dampx*vxi;
vxi=dampx*temp;



xi=pos[0];
yi=pos[1];
xi2=pos2[0];
yi2=pos2[1];

tint=5.0;
tint2=5.0;


coldelay=0.0;



}
coldelay=coldelay+1;
/*gl of line for collision and path*/

glClear(GL_COLOR_BUFFER_BIT);

glPointSize(10);
glBegin(GL_POINTS);
glColor3f(0.0,1.0,0.0);
glVertex2fv(pos);
glEnd();

glBegin(GL_POINTS);
glColor3f(1.0,0.0,0.0);
glVertex2fv(pos2);
glEnd();

glBegin(GL_LINES);
glColor3f(0.0,0.0,1.0);
glVertex2fv(vv);
glVertex2fv(xx);
glVertex2fv(hh);
glVertex2fv(xx);
glVertex2fv(jj);
glVertex2fv(vv);

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(0.0,10.0,0.0,10.0,-2.0,2.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutMainLoop();
return 0;}
