Advertisement
qtinio

chodzi taki wenszy nawet gdy nic sie nei dzieje

May 28th, 2019
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <time.h>
  3.  
  4. #include "agents.h"
  5.  
  6.  
  7.  
  8. AutoPilot::AutoPilot()
  9. {
  10. }
  11. int cel = -1;
  12. float odleglosc = -1;
  13. float odleglosc_do_przedmiotu;
  14.  
  15. void AutoPilot::AutoControl(MovableObject *obj)
  16. {
  17.     Terrain *_terrain = obj->terrain;
  18.  
  19.     float pi = 3.1415;
  20.  
  21.     //podnoszenie_przedm = 1;
  22.     Vector3 vect_local_forward = obj->state.qOrient.rotate_vector(Vector3(1, 0, 0));
  23.     Vector3 vect_local_right = obj->state.qOrient.rotate_vector(Vector3(0, 0, 1));
  24.     // TUTAJ NALEŻY UMIEŚCIĆ ALGORYTM AUTONOMICZNEGO STEROWANIA POJAZDEM
  25.     // .................................................................
  26.     // .................................................................
  27.     if (cel == -1)
  28.     {
  29.         for (int i = 0; i < _terrain->number_of_items; i++)
  30.         {
  31.             if (_terrain->p[i].to_take)
  32.             {
  33.                 odleglosc_do_przedmiotu = (_terrain->p[i].vPos - obj->state.vPos + Vector3(0, obj->state.vPos.y - _terrain->p[i].vPos.y, 0)).length();
  34.                 if (odleglosc == -1)
  35.                     odleglosc = odleglosc_do_przedmiotu;
  36.                 else
  37.                     if (odleglosc > odleglosc_do_przedmiotu &&  _terrain->p[i].to_take)
  38.                     {
  39.                         odleglosc = odleglosc_do_przedmiotu;
  40.                         cel = i;
  41.                     }
  42.             }
  43.         }
  44.     }
  45.  
  46.     if (cel > -1)
  47.     {
  48.         Vector3 x = _terrain->p[cel].vPos - obj->state.vPos;
  49.         Vector3 forward = obj->state.qOrient.rotate_vector(Vector3(1, 0, 0));// obroc_wektor(Wektor3(1, 0, 0));
  50.         Vector3 right = obj->state.qOrient.rotate_vector(Vector3(0, 1, 0));
  51.  
  52.         float cos = (forward^x) / (x.length() * forward.length());
  53.         float kat = acos(cos);
  54.         odleglosc_do_przedmiotu = (_terrain->p[cel].vPos - obj->state.vPos + Vector3(0, obj->state.vPos.y - _terrain->p[cel].vPos.y, 0)).length();
  55.  
  56.         if (!_terrain->p[cel].to_take) {
  57.             cel = -1;
  58.         }
  59.  
  60.         if (cos > 0.7)
  61.         {
  62.             obj->state.wheel_turn_angle = 0;
  63.         }
  64.         else
  65.         {
  66.  
  67.             if (!obj->if_keep_steer_wheel)
  68.             {
  69.                 if (kat > PI) {
  70.                     obj->breaking_degree = 0.4;
  71.                     obj->state.wheel_turn_angle = 0.1f;
  72.                 }
  73.                 else {
  74.                     obj->breaking_degree = 0.4;
  75.                     obj->state.wheel_turn_angle = -0.1f;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         if (odleglosc_do_przedmiotu < 30)
  81.             if (obj->state.vV.length() < 4.0)
  82.                 obj->F = 5000.0;
  83.             else
  84.                 obj->F = 0.0;
  85.         else if (odleglosc_do_przedmiotu < 200)
  86.             if (obj->state.vV.length() < 10.0)
  87.                 obj->F = 5000.0;
  88.             else
  89.                 obj->F = 0.0;
  90.         else
  91.             if (obj->state.vV.length() < 25.0)
  92.                 obj->F = 5000.0;
  93.             else
  94.                 obj->F = 0.0;
  95.        
  96.     }
  97.  
  98. }
  99.  
  100. void AutoPilot::ControlTest(MovableObject *_ob, float krok_czasowy, float czas_proby)
  101. {
  102.     bool koniec = false;
  103.     float _czas = 0;               // czas liczony od początku testu
  104.                                    //FILE *pl = fopen("test_sterowania.txt","w");
  105.     while (!koniec)
  106.     {
  107.         _ob->Simulation(krok_czasowy);
  108.         AutoControl(_ob);
  109.         _czas += krok_czasowy;
  110.         if (_czas >= czas_proby) koniec = true;
  111.         //fprintf(pl,"czas %f, vPos[%f %f %f], got %d, pal %f, F %f, wheel_turn_angle %f, breaking_degree %f\n",_czas,_ob->vPos.x,_ob->vPos.y,_ob->vPos.z,_ob->money,_ob->amount_of_fuel,_ob->F,_ob->wheel_turn_angle,_ob->breaking_degree);
  112.     }
  113.     //fclose(pl);
  114. }
  115.  
  116. // losowanie liczby z rozkladu normalnego o zadanej sredniej i wariancji
  117. float Randn(float srednia, float wariancja, long liczba_iter)
  118. {
  119.     //long liczba_iter = 10;  // im wiecej iteracji tym rozklad lepiej przyblizony
  120.     float suma = 0;
  121.     for (long i = 0; i < liczba_iter; i++)
  122.         suma += (float)rand() / RAND_MAX;
  123.     return (suma - (float)liczba_iter / 2)*sqrt(12 * wariancja / liczba_iter) + srednia;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement