Advertisement
Charlesr456

GameProg 26/11/12

Nov 26th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.03 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. double dt=0.01; // this is the time step
  9. float verysmall=0.001;
  10. float totaltime=0.0;
  11. float gravity = 1.0;
  12. GLfloat near=0.1,far=27.0;
  13. GLfloat startdisp=-1;
  14.  
  15. class ball
  16. {
  17. private:
  18. // ax acceleration in x, px position in x, vx velocity in x
  19. double ax,ay,az,px,py,pz,pxstart,pystart,pzstart,vx,vy,vz,cr,cg,cb,size,speed,anglexz,angleyz,initialspeed;
  20.  
  21. private:
  22. bool ontee;
  23.  
  24. public:
  25. ball(double pxIn, double pyIn, double pzIn, double crIn, double cgIn, double cbIn, double initialspeedIn, double angxzIn)
  26. {
  27. ontee = true;
  28. pxstart = pxIn; px = pxIn; vx = 0; cr = crIn; size = 0.1; initialspeed = initialspeedIn;
  29. pystart = pyIn; py = pyIn; vy = 0; cg = cgIn; speed = 0; anglexz = angxzIn;
  30. pzstart = pzIn; pz = pzIn; vz = 0; cb = cbIn; ay = gravity; angleyz = 0;
  31. }
  32.  
  33. double getpx(){return px;} double getvx(){return vx;}; double getcr(){return cr;}
  34. double getpy(){return py;}; double getvy(){return vy;}; double getcg(){return cg;}
  35. double getpz(){return pz;}; double getvz(){return vz;}; double getcb(){return cb;}
  36.  
  37. double getspeed(){return sqrt(py*py+px*px+pz*pz);}
  38. double getballsize(){return size;};
  39.  
  40. double setpx(double pxIn){px = pxIn;}
  41. double setpy(double pyIn){py = pyIn;}
  42. double setpz(double pzIn){pz = pzIn;}
  43.  
  44.  
  45. void move()
  46. {
  47. if(ontee==false){return;}
  48. ontee=false;
  49. // convert information into vx vy and vz
  50. double pi=3.14;
  51. initialspeed;
  52. vy = initialspeed*sin((2*pi*angleyz)/360);
  53. vz = initialspeed*cos((2*pi*angleyz/360))*cos((2*pi*anglexz/360));
  54. vx = initialspeed*cos((2*pi*angleyz/360))*sin((2*pi*anglexz/360));
  55. }
  56.  
  57.  
  58. void alterspeed(double xx)
  59. {
  60. if(ontee==false)
  61. return;
  62.  
  63. initialspeed += xx;
  64.  
  65. if(initialspeed < 0)
  66. initialspeed=0;
  67. }
  68.  
  69. void rotateinxzplane(double xx)
  70. {
  71. if(ontee==false)
  72. return;
  73. anglexz += xx;
  74. }
  75. void rotateinyzplane(double xx)
  76. {
  77. if(ontee==false)
  78. return;
  79. angleyz += xx;
  80. }
  81.  
  82. void info()
  83. {
  84. if(ontee==false)return;
  85. cout << "speed=" << initialspeed << " anglexz=" << anglexz << " angleyz=" << angleyz << "\n";
  86. double vya = initialspeed*sin(angleyz);
  87. double vxa = initialspeed*cos(angleyz)*cos(anglexz);
  88. double vza = initialspeed*cos(angleyz)*sin(anglexz);
  89. cout << "corresponds to " << "vx=" << vxa << " vy=" << vya << " vz=" << vza << "\n";
  90. cout << "corresponds to " << "px=" << px << " py=" << py << " pz=" << pz << "\n";
  91. cout << "----------------------------------\n";
  92. }
  93.  
  94. void reset()
  95. {
  96. ontee=true;
  97. px=pxstart; py=pystart; pz=pzstart; size=0.1;
  98. vx=0; vy=0; vz=0; ay=gravity;
  99. //anglexz=180;
  100. angleyz=0;
  101. }
  102.  
  103. void evolve()
  104. {
  105. if(ontee==true)
  106. {
  107. return;
  108. }
  109.  
  110. // cout << "vy" << vy << "\n";
  111. vy += (-gravity*dt);
  112. px += vx*dt; py += vy*dt; pz += vz*dt;
  113. totaltime+=dt;
  114. if(py < -0.0005)
  115. {
  116. // bounce
  117. vy=-0.5*vy; py=0.0001;
  118. }
  119. if(pz < 0.0)
  120. {
  121. cout << "The ball has passed the origin\n";
  122. reset();
  123. }
  124. }
  125. };
  126.  
  127. ball blackball ( 0, 0, 0, 0, 0.0, 0, 7, 0.0);
  128. ball orangeball( 1, 0, 23, 1, 0.5, 0, 5, 180+2.49);
  129. ball blueball ( 3, 0, 23, 0, 0.0, 1, 2, 180+7.43);
  130. ball greenball (-3, 0, 23, 0, 0.1, 0, 3, 180-7.43);
  131. ball redball (-1, 0, 23, 1, 0.0, 0, 4, 180-2.49);
  132.  
  133.  
  134. void keyboard(unsigned char key, int x, int y)
  135. {
  136. switch (key)
  137. {
  138. case 'd': // rotate right
  139. blackball.rotateinxzplane(5.0);break;
  140. case 'a': // rotate left
  141. blackball.rotateinxzplane(-5.0);break;
  142. case 'w': // rotate up
  143. blackball.rotateinyzplane(5.0);break;
  144. case 's': // rotate down
  145. blackball.rotateinyzplane(-5.0);break;
  146. case 'f': //fire
  147. blackball.move();
  148. break;
  149. case 'r': // reset
  150. blackball.reset(); break;
  151. case 'o':
  152. blackball.alterspeed(0.5); break;
  153. case 'p':
  154. blackball.alterspeed(-0.1); break;
  155. case 'i':
  156. blackball.info(); break;
  157. break;
  158. case 'q':exit(0);
  159. }
  160.  
  161. glutPostRedisplay();
  162. }
  163.  
  164. void init(void)
  165. {
  166. glClearColor(1.0,1.0,1.0,0.0);
  167.  
  168. glClearDepth(1.0);
  169. glEnable(GL_DEPTH_TEST);
  170. glDepthMask(GL_TRUE);
  171. }
  172.  
  173. void display(void)
  174. {
  175. glMatrixMode(GL_PROJECTION);
  176. glLoadIdentity();
  177. gluPerspective(40,1,near,far);
  178.  
  179. glMatrixMode(GL_MODELVIEW);
  180. glLoadIdentity();
  181. gluLookAt(0.0, 2.0, 3.0,
  182. 0.0, 0.0, -10.0,
  183. 0.0, 1.0, 0.0);
  184.  
  185. glClear(GL_COLOR_BUFFER_BIT);
  186.  
  187. glPushMatrix();
  188. glColor3f (blackball.getcr(), blackball.getcg(), blackball.getcb());
  189. glTranslatef(blackball.getpx(),blackball.getpy(),blackball.getpz());
  190. glutSolidSphere(blackball.getballsize(),20,20);
  191. glPopMatrix();
  192.  
  193. glPushMatrix();
  194. glColor3f (orangeball.getcr(), orangeball.getcg(), orangeball.getcb());
  195. glTranslatef(orangeball.getpx(),orangeball.getpy(),startdisp-orangeball.getpz());
  196. glutSolidSphere(orangeball.getballsize(),20,20);
  197. glPopMatrix();
  198.  
  199. glPushMatrix();
  200. glColor3f (blueball.getcr(), blueball.getcg(), blueball.getcb());
  201. glTranslatef(blueball.getpx(),blueball.getpy(),startdisp-blueball.getpz());
  202. glutSolidSphere(blueball.getballsize(),20,20);
  203. glPopMatrix();
  204.  
  205. glPushMatrix();
  206. glColor3f (greenball.getcr(), greenball.getcg(), greenball.getcb());
  207. glTranslatef(greenball.getpx(), greenball.getpy(),startdisp-greenball.getpz());
  208. glutSolidSphere(greenball.getballsize(),20,20);
  209. glPopMatrix();
  210.  
  211. glPushMatrix();
  212. glColor3f (redball.getcr(), redball.getcg(), redball.getcb());
  213. glTranslatef(redball.getpx(), redball.getpy(),startdisp-redball.getpz());
  214. glutSolidSphere(redball.getballsize(),20,20);
  215. glPopMatrix();
  216.  
  217. glFlush();
  218. glutSwapBuffers();
  219. }
  220.  
  221. void evolve(int a)
  222. {
  223. orangeball.move();
  224. blueball.move();
  225. greenball.move();
  226. redball.move();
  227.  
  228. blackball.evolve();
  229. orangeball.evolve();
  230. blueball.evolve();
  231. greenball.evolve();
  232. redball.evolve();
  233.  
  234. glutPostRedisplay();
  235. glutTimerFunc(20,evolve,20);
  236. }
  237.  
  238. int main(int argc, char** argv)
  239. {
  240. glutInit(&argc, argv);
  241. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  242. glutInitWindowSize (800, 800);
  243. glutCreateWindow (argv[0]);
  244. init();
  245. glutKeyboardFunc (keyboard);
  246. //glutIdleFunc(evolve);
  247. glutTimerFunc(100,evolve,100);
  248. glutDisplayFunc (display);
  249. cout << "Hello All on CG6301\n";
  250. cout << "This program models projectile motion, the user picks the direction and speed of the blackball\n";
  251. cout << "Note the following \n";
  252. cout << "`a' rotates left , `d' rotates right\n";
  253. cout << "`w' rotates up, `x' rotates down\n";
  254. cout << "`o' increases speed of shot, `p' decreases speed of shot\n";
  255. cout << "`i' info \n";
  256. cout << "`r' reset \n";
  257.  
  258. glutMainLoop();
  259. return 0;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement