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;
- //fstream output("data.txt",ios::out);
- double dt = 0.05; // this is the time step
- float coeF = 0.5;
- float verysmall = 0.001;
- float totaltime = 0.0;
- float gravity = 0.1;
- GLfloat near = 0.1,far = 20.0;
- GLfloat startdispz = -1.0;
- const float DEG2RAD = 3.14159/180;
- class ball
- {
- private:
- // ax acceleration in x, px position in x, vx velocity in x
- double ax,ay,az,px,py,pz,vx,vy,vz,size;
- public:
- ball()
- {
- px=0.0;py=0.0;
- }
- ball(double dummyvx, double dummyvy,double dummyvz)
- {
- px=0; py=0.1; pz=0; size=0.1;
- vx=dummyvx; vy=dummyvy; vz=dummyvz; ay=gravity;
- }
- double getpx(){return px;} double getpy(){return py;} double getpz(){return pz;}
- double getvx(){return vx;} double getvy(){return vy;} double getvz(){return vz;}
- double getballsize(){return size;}
- double getspeed(){return sqrt(py*py + px*px + pz*pz);}
- void evolve()
- {
- //cout << "vy" << vy << "\n";
- vy += (-gravity*dt); // equation for the y component of v
- px += vx*dt; py += vy*dt; pz += vz*dt; // equations for all 3 components of p
- totaltime += dt; // update total time
- //output << totaltime << " " << px << " " << py << " " << pz << "\n";
- //cout << totaltime << " " << py << " " << pz << "\n";
- //terminate when we hit the ground i.e pz < 0
- if(py < size)
- {
- py = size;
- vy = -vy*coeF;
- //cout << "The ball has hit the ground\n";
- //vx=0; vy=0; vz=0;
- //output.close();
- //exit(0);
- }
- }
- };
- // ball start at origin with velocity vector as in the constructor
- ball golfball(0.0, 0.4, 0.8);
- void init(void)
- {
- glClearColor(1.0,1.0,1.0,0.0);
- }
- void drawCircle(float Radius, int numPoints)
- {
- glBegin( GL_LINE_LOOP );
- for( int i=0; i<numPoints; i++ )
- {
- float Angle = i * (2.0*M_PI/numPoints);
- float X = cos( Angle )*Radius;
- float Y = sin( Angle )*Radius;
- glVertex2f( X, Y );
- }
- glEnd();
- }
- 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 (0.0, 0.0, 0.0);
- glTranslatef(golfball.getpx(),golfball.getpy(),startdispz - golfball.getpz());
- glutSolidSphere(golfball.getballsize(),20,20);
- glPopMatrix();
- glPushMatrix();
- glColor3f (0.0, 1.0, 0.0);
- glTranslatef(0,0.0,-7.5);
- glRotatef(90, 1.0, 0.0, 0.0);
- drawCircle(0.1,24);
- glRotatef(-90, 1.0, 0.0, 0.0);
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'q':
- exit(0);
- }
- }
- void evolve(int a)
- {
- // evolve system here
- golfball.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 golf ball moves under gravity(no friction)\n";
- cout << "The file is closed and the program terminates when the ball is deemed to have hit the ground \n";
- cout << "The lab questions ask you to determine how accurately we have modelled the system\n";
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement