Guest User

Untitled

a guest
Dec 3rd, 2019
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <string.h>
  7. #include <fstream>
  8. #include "mpi.h"
  9. #include <GL/glut.h>
  10. #include <cstdlib>
  11.  
  12. using namespace std;
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <GL/glut.h>
  17. #include <cstdlib>
  18. #include <iostream>
  19. #include <cmath>
  20. #include <math.h>
  21. #include <fstream>
  22. #include <chrono>
  23. #include <thread>
  24.  
  25. using namespace std;
  26.  
  27. double uavX[15], uavY[15], uavZ[15], velX[15], velY[15], velZ[15];
  28. double aMax = 20;
  29.  
  30. // Whole space is defined in meters
  31.  
  32.  
  33. double squared(double in)
  34. {
  35. return (in * in);
  36. }
  37.  
  38. double getMagnitude(double in1, double in2, double in3)
  39. {
  40. double magnitude = sqrt(squared(in1) + squared(in2) + squared(in3));
  41. return magnitude;
  42. }
  43.  
  44. double getDistance(double x1, double y1, double z1, double x2, double y2, double z2)
  45. {
  46. double magnitude = sqrt(squared((x1 - x2)) + squared((y1 - y2)) + squared((z1 - z2)) );
  47. return magnitude;
  48. }
  49.  
  50. /*
  51. * a function that determines the total speed of a UAV
  52. * @param vX: velocity in X direction
  53. * @param vY: velocity in Y direction
  54. * @param vZ: velocity in Z direction
  55. * @return: double giving total speed
  56. */
  57. double getSpeed(double vX, double vY, double vZ)
  58. {
  59. double magnitude = sqrt(vX*vX + vY*vY + vZ*vZ);
  60. return magnitude;
  61. }
  62.  
  63.  
  64. // Yards to meters
  65. double yToM(double yards)
  66. {
  67. return (yards * 0.9144);
  68. }
  69.  
  70. // Meters to yards
  71. double mToY(double meters)
  72. {
  73. return (meters * 1.09361);
  74. }
  75.  
  76. // Get spherical coordinates
  77. double getPhi(double uavX, double uavY, double uavZ)
  78. {
  79. double numerator = sqrt(squared(uavX) + squared(uavY));
  80. double phi = atan2(numerator, 50.0);
  81. return phi;
  82. }
  83.  
  84. double getTheta(double uavX, double uavY)
  85. {
  86. if (uavX == 0 && uavY == 0)
  87. {
  88. return 0.0;
  89. }
  90. else
  91. {
  92. return atan2(-uavY, -uavX);
  93. }
  94.  
  95. }
  96.  
  97. double newVelo(double origV, double acc)
  98. {
  99. return (origV + acc);
  100. }
  101.  
  102. void init(void)
  103. {
  104. // Set initial parameters
  105. glDepthMask(GL_TRUE);
  106. glMatrixMode(GL_PROJECTION);
  107.  
  108. // Set black background
  109. glClearColor(0.0, 0.0, 0.0, 0.0);
  110.  
  111. // Set smooth objects and maintain colors
  112. glShadeModel(GL_SMOOTH);
  113. glEnable(GL_LIGHTING);
  114. glEnable(GL_COLOR_MATERIAL);
  115. glEnable(GL_DEPTH_TEST);
  116. }
  117.  
  118.  
  119. /*
  120. * This function will define the initial perspective and
  121. * viewport for the assignment.
  122. * No parameters and no return values.
  123. */
  124. void resize(int w, int h)
  125. {
  126. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  127. glMatrixMode(GL_PROJECTION);
  128. glLoadIdentity();
  129. gluPerspective(45.0, ((w + 0.0) / (h + 0.0)), 1.0, 200);
  130. }
  131.  
  132.  
  133. void display(void)
  134. {
  135. int count = 0;
  136. double distance = 0;
  137. double phi, theta;
  138. double xPos, yPos, zPos, xVel[15], yVel[15], zVel[15];
  139. double vMag, xVel0, yVel0, zVel0;
  140. double vel1[15], vel2[15], vel3[15];
  141. double aX, aY, aZ;
  142.  
  143. // Setup beginning uav values
  144. for (int j = 0; j < 5; ++j)
  145. {
  146. for (int i = 0; i < 3; ++i)
  147. {
  148. uavX[count] = yToM(26.667 - (26.667 * i));
  149. uavY[count] = yToM(-50.0 + (25 * j));
  150. uavZ[count] = 0;
  151. xVel[count] = 0;
  152. yVel[count] = 0;
  153. zVel[count] = 0;
  154. ++count;
  155. }
  156. }
  157.  
  158. count = 0;
  159.  
  160. for (int time = 0; time < 1000; ++time)
  161. {
  162. // Clear and set matrix mode
  163. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  164. glMatrixMode(GL_MODELVIEW);
  165.  
  166. // Draw scene and look at football field
  167. glLoadIdentity();
  168. gluLookAt(120.0, 0.0, 120.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  169.  
  170. // Draw the football field
  171. glColor3f(0.0, 1.0, 0.0);
  172. glPushMatrix();
  173. glBegin(GL_POLYGON);
  174. glTranslatef(0.0, 0.0, 0.0);
  175. glVertex3f(yToM(-26.667), yToM(-50.0), 0.0);
  176. glVertex3f(yToM(-26.667), yToM(50.0), 0.0);
  177. glVertex3f(yToM(26.667), yToM(50.0), 0.0);
  178. glVertex3f(yToM(26.667), yToM(-50.0), 0.0);
  179. glEnd();
  180.  
  181. glColor3f(1.0, 1.0, 0.0);
  182. glPushMatrix();
  183. glTranslatef(0.0, 0.0, 50.0);
  184. glutWireSphere(10.0, 50.0, 50.0);
  185. glPopMatrix();
  186.  
  187. glColor3f(1.0, 0.0, 0.0);
  188.  
  189. count = 0;
  190.  
  191. for (int c = 0; c < 15; ++c)
  192. {
  193. xPos = uavX[c];
  194. yPos = uavY[c];
  195. zPos = uavZ[c];
  196. glPushMatrix();
  197. glTranslatef(xPos, yPos, zPos);
  198. glutSolidTetrahedron();
  199. glPopMatrix();
  200.  
  201. vMag = getMagnitude(velX[c], velY[c], velZ[c]);
  202. distance = getDistance(xPos, yPos, zPos, 0.0, 0.0, 50.0);
  203. if (distance <= 10)
  204. {
  205. // MAKE REALISTIC!!!!
  206. velX[c] -= velX[c];
  207. velY[c] -= velY[c];
  208. velZ[c] -= velZ[c];
  209. }
  210. else
  211. {
  212. if (vMag < 0.001)
  213. {
  214. phi = getPhi(xPos, yPos, zPos);
  215. theta = getTheta(xPos, yPos);
  216. aX = 10 * sin(phi) * cos(theta);
  217. aY = 10 * sin(phi) * sin(theta);
  218. aZ = 10 * cos(phi) + 10;
  219. aZ -= 10;
  220. uavX[c] = xPos + (velX[c] * 0.1) + (aX * 0.1 * 0.1 / 2);
  221. uavY[c] = yPos + (velY[c] * 0.1) + (aY * 0.1 * 0.1 / 2);
  222. uavZ[c] = zPos + (velZ[c] * 0.1) + (aZ * 0.1 * 0.1 / 2);
  223. velX[c] = aX * 0.1;
  224. velY[c] = aY * 0.1;
  225. velZ[c] = aZ * 0.1;
  226. }
  227. else
  228. {
  229. uavX[c] = xPos + (velX[c] * 0.1);
  230. uavY[c] = yPos + (velY[c] * 0.1);
  231. uavZ[c] = zPos + (velZ[c] * 0.1);
  232. }
  233. }
  234. }
  235. glutSwapBuffers();
  236. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  237. }
  238.  
  239. }
  240.  
  241.  
  242. void change(void)
  243. {
  244. glutPostRedisplay();
  245. }
  246.  
  247.  
  248.  
  249.  
  250.  
  251. int main(int argc, char **argv)
  252. {
  253. glutInit(&argc, argv);
  254. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  255.  
  256. // Define initial position and look angle
  257. glutInitWindowPosition(100,100);
  258. glutInitWindowSize(400,400);
  259. glutCreateWindow ("ECE 4122 Final Project");
  260. glEnable(GL_DEPTH_TEST);
  261.  
  262. init();
  263. glutReshapeFunc(resize);
  264. glutDisplayFunc(display);
  265. glutMainLoop();
  266.  
  267. return 0;
  268. }
RAW Paste Data