Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void timer(int v){
- int time = glutGet(GLUT_ELAPSED_TIME); // In msec
- //cout<<"time: "<<glutGet(GLUT_ELAPSED_TIME)<<endl;
- // cout<<"\n"<<endl;
- float angle;
- // Set up next timer event
- glutTimerFunc(60, timer, time);
- if (animating) {
- int delta_t = time - v;
- float delta_x, delta_z, length, step_size;
- // Compute vector from current location to mouse
- delta_x = 10000 - roboX;
- delta_z = 10000 - roboZ;
- //cout<<roboX<<endl;
- //cout<<roboZ<<endl;
- //cout<<delta_x<<endl;
- //cout<<delta_z<<endl;
- // Compute length of the vector
- length = sqrt (delta_x*delta_x + delta_z*delta_z);
- //cout<<length<<endl;
- step_size = 0.1 * delta_t;
- //cout<<"step ";
- //cout<<step_size<<endl;
- //cout<<"stepcalc ";
- //cout<<step_size*0.55<<endl;
- if (length > step_size * 0.55) {
- //cout<<length<<endl;
- //cout<<"true"<<endl;
- // Normalise the delta vector and compute the step
- delta_x = delta_x / length;
- delta_z = delta_z / length;
- cout<<delta_x<<endl;
- cout<<delta_z<<endl;
- angle = atan2(delta_z, delta_x);
- // Convert radians to degrees.
- //calculates the difference in angle between the direction the buggy is
- //facing and the direction of the mouse from the buggy
- float angTarget = angle * 180.0 / PI;
- cout<<"ang ";
- cout<<angTarget<<endl;
- float diff = angTarget-roboRot;
- cout<<"diff ";
- cout<<diff<<endl;
- //limiters to prevent the buggy from
- //rotating past 180 or -180 degrees
- if(diff<-180){
- diff+=360;
- }
- if(diff>180){
- diff-=360;
- }
- if(roboRot<-180){
- roboRot+=360;
- }
- if(roboRot>180){
- roboRot-=360;
- }
- if(diff<-0.3){
- diff = -0.3;
- }
- else if(diff>0.3){
- diff = 0.3;
- }
- roboRot +=diff;
- cout<<"robo ";
- cout<<roboRot<<endl;
- if(diff ==0){
- roboX += delta_x * step_size;
- roboZ += delta_z * step_size;
- }
- glutPostRedisplay();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement