Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <math.h>
- #include <iostream>
- #include <fstream>
- using namespace std;
- double dt=0.01; // this is the time step
- float verysmall=0.001;
- float totaltime=0.0;
- float gravity = 1.0;
- GLfloat near=0.1,far=27.0;
- GLfloat startdisp=-1;
- class ball
- {
- private:
- // ax acceleration in x, px position in x, vx velocity in x
- double ax,ay,az,px,py,pz,pxstart,pystart,pzstart,vx,vy,vz,cr,cg,cb,size,speed,anglexz,angleyz,initialspeed;
- private:
- bool ontee;
- public:
- ball(double pxIn, double pyIn, double pzIn, double crIn, double cgIn, double cbIn, double initialspeedIn, double angxzIn)
- {
- ontee = true;
- pxstart = pxIn; px = pxIn; vx = 0; cr = crIn; size = 0.1; initialspeed = initialspeedIn;
- pystart = pyIn; py = pyIn; vy = 0; cg = cgIn; speed = 0; anglexz = angxzIn;
- pzstart = pzIn; pz = pzIn; vz = 0; cb = cbIn; ay = gravity; angleyz = 0;
- }
- double getpx(){return px;} double getvx(){return vx;}; double getcr(){return cr;}
- double getpy(){return py;}; double getvy(){return vy;}; double getcg(){return cg;}
- double getpz(){return pz;}; double getvz(){return vz;}; double getcb(){return cb;}
- double getspeed(){return sqrt(py*py+px*px+pz*pz);}
- double getballsize(){return size;};
- double setpx(double pxIn){px = pxIn;}
- double setpy(double pyIn){py = pyIn;}
- double setpz(double pzIn){pz = pzIn;}
- void move()
- {
- if(ontee==false){return;}
- ontee=false;
- // convert information into vx vy and vz
- double pi=3.14;
- initialspeed;
- vy = initialspeed*sin((2*pi*angleyz)/360);
- vz = initialspeed*cos((2*pi*angleyz/360))*cos((2*pi*anglexz/360));
- vx = initialspeed*cos((2*pi*angleyz/360))*sin((2*pi*anglexz/360));
- }
- void alterspeed(double xx)
- {
- if(ontee==false)
- return;
- initialspeed += xx;
- if(initialspeed < 0)
- initialspeed=0;
- }
- void rotateinxzplane(double xx)
- {
- if(ontee==false)
- return;
- anglexz += xx;
- }
- void rotateinyzplane(double xx)
- {
- if(ontee==false)
- return;
- angleyz += xx;
- }
- void info()
- {
- if(ontee==false)return;
- cout << "speed=" << initialspeed << " anglexz=" << anglexz << " angleyz=" << angleyz << "\n";
- double vya = initialspeed*sin(angleyz);
- double vxa = initialspeed*cos(angleyz)*cos(anglexz);
- double vza = initialspeed*cos(angleyz)*sin(anglexz);
- cout << "corresponds to " << "vx=" << vxa << " vy=" << vya << " vz=" << vza << "\n";
- cout << "corresponds to " << "px=" << px << " py=" << py << " pz=" << pz << "\n";
- cout << "----------------------------------\n";
- }
- void reset()
- {
- ontee=true;
- px=pxstart; py=pystart; pz=pzstart; size=0.1;
- vx=0; vy=0; vz=0; ay=gravity;
- //anglexz=180;
- angleyz=0;
- }
- void evolve()
- {
- if(ontee==true)
- {
- return;
- }
- // cout << "vy" << vy << "\n";
- vy += (-gravity*dt);
- px += vx*dt; py += vy*dt; pz += vz*dt;
- totaltime+=dt;
- if(py < -0.0005)
- {
- // bounce
- vy=-0.5*vy; py=0.0001;
- }
- if(pz < 0.0)
- {
- cout << "The ball has passed the origin\n";
- reset();
- }
- }
- };
- ball blackball ( 0, 0, 0, 0, 0.0, 0, 7, 0.0);
- ball orangeball( 1, 0, 23, 1, 0.5, 0, 5, 180+2.49);
- ball blueball ( 3, 0, 23, 0, 0.0, 1, 2, 180+7.43);
- ball greenball (-3, 0, 23, 0, 0.1, 0, 3, 180-7.43);
- ball redball (-1, 0, 23, 1, 0.0, 0, 4, 180-2.49);
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'd': // rotate right
- blackball.rotateinxzplane(5.0);break;
- case 'a': // rotate left
- blackball.rotateinxzplane(-5.0);break;
- case 'w': // rotate up
- blackball.rotateinyzplane(5.0);break;
- case 's': // rotate down
- blackball.rotateinyzplane(-5.0);break;
- case 'f': //fire
- blackball.move();
- break;
- case 'r': // reset
- blackball.reset(); break;
- case 'o':
- blackball.alterspeed(0.5); break;
- case 'p':
- blackball.alterspeed(-0.1); break;
- case 'i':
- blackball.info(); break;
- break;
- case 'q':exit(0);
- }
- glutPostRedisplay();
- }
- void init(void)
- {
- glClearColor(1.0,1.0,1.0,0.0);
- glClearDepth(1.0);
- glEnable(GL_DEPTH_TEST);
- glDepthMask(GL_TRUE);
- }
- void display(void)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(40,1,near,far);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0, 2.0, 3.0,
- 0.0, 0.0, -10.0,
- 0.0, 1.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix();
- glColor3f (blackball.getcr(), blackball.getcg(), blackball.getcb());
- glTranslatef(blackball.getpx(),blackball.getpy(),blackball.getpz());
- glutSolidSphere(blackball.getballsize(),20,20);
- glPopMatrix();
- glPushMatrix();
- glColor3f (orangeball.getcr(), orangeball.getcg(), orangeball.getcb());
- glTranslatef(orangeball.getpx(),orangeball.getpy(),startdisp-orangeball.getpz());
- glutSolidSphere(orangeball.getballsize(),20,20);
- glPopMatrix();
- glPushMatrix();
- glColor3f (blueball.getcr(), blueball.getcg(), blueball.getcb());
- glTranslatef(blueball.getpx(),blueball.getpy(),startdisp-blueball.getpz());
- glutSolidSphere(blueball.getballsize(),20,20);
- glPopMatrix();
- glPushMatrix();
- glColor3f (greenball.getcr(), greenball.getcg(), greenball.getcb());
- glTranslatef(greenball.getpx(), greenball.getpy(),startdisp-greenball.getpz());
- glutSolidSphere(greenball.getballsize(),20,20);
- glPopMatrix();
- glPushMatrix();
- glColor3f (redball.getcr(), redball.getcg(), redball.getcb());
- glTranslatef(redball.getpx(), redball.getpy(),startdisp-redball.getpz());
- glutSolidSphere(redball.getballsize(),20,20);
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- }
- void evolve(int a)
- {
- orangeball.move();
- blueball.move();
- greenball.move();
- redball.move();
- blackball.evolve();
- orangeball.evolve();
- blueball.evolve();
- greenball.evolve();
- redball.evolve();
- glutPostRedisplay();
- glutTimerFunc(20,evolve,20);
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize (800, 800);
- glutCreateWindow (argv[0]);
- init();
- glutKeyboardFunc (keyboard);
- //glutIdleFunc(evolve);
- glutTimerFunc(100,evolve,100);
- glutDisplayFunc (display);
- cout << "Hello All on CG6301\n";
- cout << "This program models projectile motion, the user picks the direction and speed of the blackball\n";
- cout << "Note the following \n";
- cout << "`a' rotates left , `d' rotates right\n";
- cout << "`w' rotates up, `x' rotates down\n";
- cout << "`o' increases speed of shot, `p' decreases speed of shot\n";
- cout << "`i' info \n";
- cout << "`r' reset \n";
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement