Single Launch Code

WB01343_.gif (599 bytes)

#include<GL/glut.h>
#include<iostream.h>
#include<math.h>

double v, ang, tdist, range, t=0, tstep=0.0001, a=32;

const double PI=3.141592653, G=1;

const double objx = 500, objy = 200, objm = 150, pm = 10;

void init() {
  glClearColor(0,0,0,0);
  glShadeModel(GL_FLAT);
}

void display() {
  double vx=v*cos(ang*PI/180), vy=v*sin(ang*PI/180);
  double x=0, y=0;

  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity();
  glColor3f(1,1,0);
  glBegin(GL_QUADS);
  glVertex3f(tdist-range, 0, 0);
  glVertex3f(tdist+range, 0, 0);
  glVertex3f(tdist+range, tdist/40, 0);
  glVertex3f(tdist-range, tdist/40, 0);
  glEnd();

  // Draws point force - comment out if you want
  glColor3f(1,0,1);
  glBegin(GL_QUADS);
  glVertex3f(objx-2, objy-2, 0);
  glVertex3f(objx+2, objy-2, 0);
  glVertex3f(objx+2, objy+2, 0);
  glVertex3f(objx-2, objy+2, 0);
  glEnd();
  
  glColor3f(0,1,1);
  glBegin(GL_POINTS);
  
  do {
    t+=tstep;

    // Comment the next two lines out to remove the point force 
    vx+=G*objm*pm*(objx-x)/pow(pow(objx-x,2)+pow(objy-y,2),3.0/2);
    vy+=G*objm*pm*(objy-y)/pow(pow(objx-x,2)+pow(objy-y,2),3.0/2);

    vy-=a*tstep;

    y+=vy*tstep;
    x+=vx*tstep;
    glVertex3f(x,y,0); 
  } while (y>0 && y<1000);

  glEnd();
  cout << ((x > tdist-range && x < tdist+range) ? "Hit" : "Miss") << endl; 
}

void reshape(int w, int h) {
  glViewport(0, 0, GLsizei(w), GLsizei(h));
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, tdist*1.2, 0, tdist*1.2, -1, 1);
  glMatrixMode(GL_MODELVIEW);
}

void getData() {
  cout << "Velocity: ";
  cin >> v;
  cout << "Launch angle: ";
  cin >> ang;
  cout << "Target distance: ";
  cin >> tdist;
  cout << "Hit radius: ";
  cin >> range;
}

void main(int argc, char* argv[]) {
  getData();  
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(500, 500);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("Projectile Prototype");
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutMainLoop();
}  
WB01343_.gif (599 bytes)