Advertisement
Guest User

Untitled

a guest
May 29th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. void timer(int v){
  2.  
  3.     int time = glutGet(GLUT_ELAPSED_TIME); // In msec
  4.     //cout<<"time:   "<<glutGet(GLUT_ELAPSED_TIME)<<endl;
  5.     //  cout<<"\n"<<endl;
  6.     float angle;
  7.     // Set up next timer event
  8.     glutTimerFunc(60, timer, time);
  9.  
  10.     if (animating) {
  11.  
  12.         int delta_t = time - v;
  13.         float delta_x, delta_z, length, step_size;
  14.         // Compute vector from current location to mouse
  15.         delta_x = 10000 - roboX;
  16.         delta_z = 10000 - roboZ;
  17.         //cout<<roboX<<endl;
  18.         //cout<<roboZ<<endl;
  19.  
  20.         //cout<<delta_x<<endl;
  21.         //cout<<delta_z<<endl;
  22.  
  23.         // Compute length of the vector
  24.         length = sqrt (delta_x*delta_x + delta_z*delta_z);
  25.         //cout<<length<<endl;
  26.         step_size = 0.1 * delta_t;
  27.         //cout<<"step ";
  28.         //cout<<step_size<<endl;
  29.  
  30.         //cout<<"stepcalc ";
  31.         //cout<<step_size*0.55<<endl;
  32.  
  33.         if (length > step_size * 0.55) {
  34.             //cout<<length<<endl;
  35.             //cout<<"true"<<endl;
  36.             // Normalise the delta vector and compute the step
  37.             delta_x = delta_x / length;
  38.             delta_z = delta_z / length;
  39.             cout<<delta_x<<endl;
  40.             cout<<delta_z<<endl;
  41.  
  42.  
  43.             angle = atan2(delta_z, delta_x);
  44.             // Convert radians to degrees.
  45.  
  46.             //calculates the difference in angle between the direction the buggy is
  47.             //facing and the direction of the mouse from the buggy
  48.             float angTarget =  angle * 180.0 / PI;
  49.             cout<<"ang ";
  50.             cout<<angTarget<<endl;
  51.  
  52.             float diff = angTarget-roboRot;
  53.             cout<<"diff ";
  54.             cout<<diff<<endl;
  55.  
  56.  
  57.             //limiters to prevent the buggy from
  58.             //rotating past 180 or -180 degrees
  59.             if(diff<-180){
  60.                 diff+=360;
  61.             }
  62.             if(diff>180){
  63.                 diff-=360;
  64.             }
  65.             if(roboRot<-180){
  66.                 roboRot+=360;
  67.             }
  68.             if(roboRot>180){
  69.                 roboRot-=360;
  70.             }
  71.  
  72.             if(diff<-0.3){
  73.                 diff = -0.3;
  74.             }
  75.             else if(diff>0.3){
  76.                 diff = 0.3;
  77.             }
  78.  
  79.             roboRot +=diff;
  80.             cout<<"robo ";
  81.             cout<<roboRot<<endl;
  82.  
  83.             if(diff ==0){
  84.  
  85.                 roboX += delta_x * step_size;
  86.                 roboZ += delta_z * step_size;
  87.             }
  88.             glutPostRedisplay();
  89.  
  90.         }
  91.  
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement