Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 495 for code/branches/AI


Ignore:
Timestamp:
Dec 12, 2007, 10:37:06 PM (17 years ago)
Author:
motth
Message:

fixed a memory leak, added documentation

Location:
code/branches/AI/src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AI/src/AIClass.h

    r426 r495  
    1 #ifndef Flocking_Class
    2 #define Flocking_Class
     1#ifndef AI_Class
     2#define AI_Class
    33
    44#include <Ogre.h>
  • code/branches/AI/src/Arrival.h

    r426 r495  
    33
    44
    5 #ifndef Flocking_Class
    6 #define Flocking_Class
     5#ifndef Arrival_Class
     6#define Arrical_Class
    77
    88#include <Ogre.h>
     
    5959
    6060  double relativeDirectApproach() {
     61    // Maxspeed / accelerationForwards = time needed to break with max acceleration
     62    // 2*getDistance()length/(MaxSpeed/accelerationForwards)^2 = required acceleration to arrive at the target with speed = 0
    6163    return (accelerationForwards / (2*getDirection().length / (MaxSpeed/accelerationForwards)^2) );
    6264  }
     
    6567    Quaternion rotation = (0,0,0,0);
    6668    if (relativeDirectApproach() > 1) {
    67       rotation = speed.getRotationTo(getDirection());
    68       // do that turn
    69      
     69      float length = speed.length();
     70      speed = (speed+getDirection());
     71      speed.normalise();
     72      speed = speed*length;
     73      if (relativeDirectApproach > 4) {
     74        //accelerate
     75      }
     76      else {
     77        // speed will stay constant
     78      }
     79
     80
    7081    }
    7182    else {
     83
    7284
    7385    }
  • code/branches/AI/src/Flocking.h

    r426 r495  
    1 //
    2 //
    3 //      TODO: testing orxonox -flocking interface
    4 //            testing algorithm
    51
    6 // ueberpruefen ob vektoren relativ richtig berechnet werden
    7 //
    8 //My Flocking Class
     2//Headerfile: Flocking.h
    93
    104#ifndef Flocking_Class
     
    2014#endif
    2115
    22 using namespace std;
    2316using namespace Ogre;
    2417
     
    3023    Vector3 speed;  // speedvector of the element
    3124    Vector3 acceleration;  // accelerationvector of the element
    32     bool movable;  // movability of the element
     25    bool movable;  // movability of the element, (false) gives the possiblity that an object can`t be moved by flocking but still gets into the calculation
     26    static int const SEPERATIONDISTANCE = 300;  //detectionradius of seperation
     27    static int const ALIGNMENTDISTANCE = 300;  //detectionradius of alignment
     28    static int const COHESIONDISTANCE = 5000;  //detectionradius of cohesion
     29    static int const ANZELEMENTS = 9;  //number of elements
    3330
     31  //default constructor
    3432  Element() {
    3533    acceleration = (0,0,0);
     
    3937  }
    4038
     39  //constructor
    4140  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
    4241    acceleration = acceleration_;
     
    4645  }
    4746
     47  //function to chance values of an element
    4848  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
    4949    acceleration = acceleration_;
     
    5555  //calculates the distance between the element and an other point given by temp
    5656  float getDistance(Element temp) {
    57     Vector3 distance = temp.location-location;  //this doesn't work
     57    Vector3 distance = temp.location-location;
    5858    return distance.length();
    5959  }
    6060
    61 //EINFÜGEN DES ELEMENTS
    62   void update(Element arrayOfElements[], const FrameEvent& time) {
    63       if (this->movable == true) {calculateAcceleration(arrayOfElements);}
    64 
    65  /*   if (this->movable ==  true) {
    66       calculateAcceleration(arrayOfElements);  //updates the acceleration
    67       calculateSpeed(time);  //updates the speed
    68       calculateLocation(time);  //updates the location
    69     }   */
     61  //updates the data of an element
     62  void update(Element arrayOfElements[]) {
     63    if (this->movable == true) {calculateAcceleration(arrayOfElements);} //if element is movable, calculate acceleration
    7064  }
    7165
    72 //EINFÜGEN DES ELEMENTS
     66  //calculates the new acceleration of an element
    7367  void calculateAcceleration(Element arrayOfElements[]) {
    74   //calculates the accelerationvector based on the steeringvectors of
    75   //separtion, alignment and cohesion.
    76   acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);
     68    acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);  //acceleration consisting of flocking-functions
    7769  }
    7870
    79   void calculateSpeed(const FrameEvent& time) {
    80     speed = speed + acceleration*time.timeSinceLastFrame;
     71  //separation-function (keep elements separated, avoid crashs)
     72  Vector3 separation(Element arrayOfElements[]) {
     73    Vector3 steering = Vector3(0,0,0); //steeringvector
     74    Vector3 inverseDistance = Vector3(0,0,0);  //vector pointing away from possible collisions
     75    int numberOfNeighbour = 0;  //number of observed neighbours
     76    float distance = 0;  // distance to the actual element
     77    for(int i=0; i<ANZELEMENTS; i++) {  //go through all elements
     78      Element actual = arrayOfElements[i];  //get the actual element
     79      distance = getDistance(actual);  //get distance between this and actual
     80      if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
     81        inverseDistance = (0,0,0);
     82        inverseDistance = location-actual.location;  //calculate the distancevector heading towards this
     83        //adaptation of the inverseDistance to the distance
     84        if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
     85        if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;}
     86        if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;}
     87        if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;}
     88        steering = steering + inverseDistance;  //add up all significant steeringvectors
     89        numberOfNeighbour++;  //counts the elements inside the detectionradius
     90      }
     91    }
     92    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
     93    return steering;
    8194  }
    8295
    83   void calculateLocation(const FrameEvent& time) {
    84     location = location + speed*time.timeSinceLastFrame;
    85   }
    86 
    87 
    88   Vector3 separation(Element arrayOfElements[]) {
    89     Vector3* steering = new Vector3(0,0,0); //steeringvector
    90     Vector3* inverseDistance = new Vector3(0,0,0);
     96  //alignment-function (lead elements to the same heading)
     97  Vector3 alignment(Element arrayOfElements[]) {
     98    Vector3 steering = Vector3(0,0,0); //steeringvector
    9199    int numberOfNeighbour = 0;  //number of observed neighbours
    92100    float distance = 0;
    93101    //go through all elements
    94     for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
     102    for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
    95103      Element actual = arrayOfElements[i];  //get the actual element
    96       distance = getDistance(actual);  //get distance between this and actual
    97 //DUMMY SEPERATION DETECTION DISTANCE =100
    98       if ((distance > 0) && (distance < 200)) {  //do only if actual is inside detectionradius
    99         *inverseDistance = (0,0,0);
    100         *inverseDistance = location-actual.location;  //calculate the distancevector heading towards this
    101         //*inverseDistance = inverseDistance->normalise(); //does this work correctly?  //normalise the distancevector
    102         if ((distance < 100) && (distance >= 80)) {*inverseDistance = *inverseDistance*2;}
    103         if ((distance < 80) && (distance >= 60)) {*inverseDistance = *inverseDistance*5;}
    104         if ((distance < 60) && (distance >= 40)) {*inverseDistance = *inverseDistance*10;}
    105         if ((distance < 40) && (distance > 0)) {*inverseDistance = *inverseDistance*20;}
    106       //  *inverseDistance = *inverseDistance/distance;  //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)
    107         *steering = *steering + *inverseDistance;  //add up all significant steeringvectors
     104      float distance = getDistance(actual);  //get distance between this and actual
     105      if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
     106        steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
     107        numberOfNeighbour++;  //counts the elements inside the detectionradius
     108      }
     109    }
     110    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
     111    return steering;
     112  }
     113
     114  //cohseion-function (keep elements close to each other)
     115  Vector3 cohesion(Element arrayOfElements[]) {
     116    Vector3 steering = Vector3(0,0,0); //steeringvector
     117    int numberOfNeighbour = 0;  //number of observed neighbours
     118    float distance = 0;
     119    //go through all elements
     120    for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
     121      Element actual = arrayOfElements[i];  //get the actual element
     122      float distance = getDistance(actual);  //get distance between this and actual
     123      if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
     124        steering = steering + actual.location;  //add up all locations of elements inside the detectionradius
    108125        numberOfNeighbour++;  //counts the elements inside the detectionradius
    109126      }
    110127    }
    111128    if(numberOfNeighbour > 0) {
    112     *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
     129      steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
     130      steering = steering - this->location;  //transform the vector for the ship
    113131    }
    114     cout<<*steering<<endl;
    115     return *steering;
     132    return steering;
    116133  }
    117 
    118   Vector3 alignment(Element arrayOfElements[]) {
    119     Vector3* steering = new Vector3(0,0,0); //steeringvector
    120     int numberOfNeighbour = 0;  //number of observed neighbours
    121     float distance = 0;
    122     //go through all elements
    123     for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
    124       Element actual = arrayOfElements[i];  //get the actual element
    125       float distance = getDistance(actual);  //get distance between this and actual
    126 //DUMMY ALIGNMENT DETECTION DISTANCE = 1000
    127       if ((distance > 0) && (distance < 300)) {  //check if actual element is inside detectionradius
    128         *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
    129         numberOfNeighbour++;  //counts the elements inside the detectionradius
    130       }
    131     }
    132     if(numberOfNeighbour > 0) {
    133     *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
    134     }
    135     return *steering;
    136   }
    137 
    138   Vector3 cohesion(Element arrayOfElements[]) {
    139     Vector3* steering = new Vector3(0,0,0); //steeringvector
    140     int numberOfNeighbour = 0;  //number of observed neighbours
    141     float distance = 0;
    142     //go through all elements
    143     for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
    144       Element actual = arrayOfElements[i];  //get the actual element
    145       float distance = getDistance(actual);  //get distance between this and actual
    146 // DUMMY COHESION DETECTION DISTANCE = 1000
    147       if ((distance > 0) && (distance < 5000)) {  //check if actual element is inside detectionradius
    148         *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
    149         numberOfNeighbour++;  //counts the elements inside the detectionradius
    150       }
    151      }
    152     if(numberOfNeighbour > 0) {
    153     *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
    154     *steering = *steering - this->location;  // (?) Koordinatensystem?
    155     }
    156     return *steering;
    157   }
    158 };
    159 
    160 
    161 
    162 //End of My Flocking Class
     134};     //End of class Element
  • code/branches/AI/src/orxonox.cc

    r468 r495  
    4444#include "loader/LevelLoader.h"
    4545#include "Flocking.h"
    46 #include "AIClass.h"
     46#include "Wander.h"
    4747
    4848// some tests to see if enet works without includsion
     
    8686
    8787Element arrayOfElements[9];
    88 
    89  // float time = 0;
     88Wander walker;
     89int counter = 0;
     90int times = 0;
     91
    9092
    9193
     
    110112      SceneManager *mgr = root_->getSceneManager("Default SceneManager");
    111113
    112       arrayOfElements[0].update(arrayOfElements, evt);
    113       arrayOfElements[1].update(arrayOfElements, evt);
    114       arrayOfElements[2].update(arrayOfElements, evt);
    115       arrayOfElements[3].update(arrayOfElements, evt);
    116       arrayOfElements[4].update(arrayOfElements, evt);
    117       arrayOfElements[5].update(arrayOfElements, evt);
    118       arrayOfElements[6].update(arrayOfElements, evt);
    119       arrayOfElements[7].update(arrayOfElements, evt);
    120       arrayOfElements[8].update(arrayOfElements, evt);
    121 
    122  /*   arrayOfElements[0].update(arrayOfElements, evt);
    123       arrayOfElements[1].update(arrayOfElements, evt);
    124       arrayOfElements[2].update(arrayOfElements, evt);
    125       arrayOfElements[3].update(arrayOfElements, evt);
    126       arrayOfElements[4].update(arrayOfElements, evt);   */
    127 
     114/*    // RUN WANDER
     115
     116      walker.update();
     117      walker.speed = walker.speed + 10*walker.acceleration*evt.timeSinceLastFrame;
     118      walker.location = walker.location + 10*walker.speed*evt.timeSinceLastFrame;
     119      walker.acceleration  = (0,0,0);
     120      mgr->getSceneNode("HeadNode10")->setPosition(walker.location);
     121
     122*/   // END RUN WANDER
     123
     124
     125
     126
     127     //  RUN FLOCKING
     128
     129      arrayOfElements[8].location = 100*Vector3(Math::Cos(Math::DegreesToRadians(counter)/10),Math::Sin(Math::DegreesToRadians(counter)/10),Math::Cos(Math::DegreesToRadians(counter+(counter-180)/2)/10));
     130
     131      arrayOfElements[0].update(arrayOfElements);
     132      arrayOfElements[1].update(arrayOfElements);
     133      arrayOfElements[2].update(arrayOfElements);
     134      arrayOfElements[3].update(arrayOfElements);
     135      arrayOfElements[4].update(arrayOfElements);
     136      arrayOfElements[5].update(arrayOfElements);
     137      arrayOfElements[6].update(arrayOfElements);
     138      arrayOfElements[7].update(arrayOfElements);
     139      arrayOfElements[8].update(arrayOfElements);
    128140
    129141      for(int i=0; i<9; i++) {
    130 
    131          arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame;
    132 
    133          arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame;
    134 
    135          arrayOfElements[i].acceleration  = (0,0,0);
     142        arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame;
     143        arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame;
     144        arrayOfElements[i].acceleration  = (0,0,0);
    136145      }
    137146
     
    146155      mgr->getSceneNode("HeadNode9")->setPosition(arrayOfElements[8].location);
    147156
    148 
    149       /*
    150 
    151       mgr->getSceneNode("HeadNode9")->setPosition(Vector3(200*cos(10*time),0,0));
    152       time = time + evt.timeSinceLastFrame;
    153 
    154      */
    155 
    156 
    157 
    158     //  mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame);
     157      counter = counter + 1;
     158      counter = counter%7200;
     159
     160      // END RUN FLOCKING
     161
    159162    }
    160163
     
    272275      cam->lookAt(Vector3(0,0,0));
    273276      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
    274       example();  //my stuff
     277
     278      //Invoke example to test AI
     279      example();
    275280    }
    276281
     
    321326    }
    322327
    323     //declaration of the 3 Ogreheads
    324    //muss leider global sein.....
    325     //Element* arrayOfElements[2];
    326 
    327328    void example() {
    328329    SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
    329330    mgr->setAmbientLight(ColourValue(1.0,1.0,1.0));
     331
     332/*  //TEST DATA WANDER
     333
     334    Entity* ent10 = mgr->createEntity("Head10", "ogrehead.mesh");
     335    SceneNode *node10 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode10", Vector3(0,0,0));
     336    node10->attachObject(ent10);
     337    Vector3 temp;
     338    temp = (0,0,0);
     339    walker.setValues(node10->getPosition(),temp,temp,true);
     340
     341*/  //END TEST DATA WANDER
     342
     343
     344//   TEST DATA FLOCKING
    330345
    331346    Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh");
     
    347362    SceneNode *node7 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode7", Vector3(-150,-150,0));
    348363    SceneNode *node8 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode8", Vector3(-150,150,0));
    349     SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0)); 
    350 
    351 // follwing camera
    352 
    353  //  Camera *cam = mgr->getCamera("Camera");
    354  //  node1->attachObject(cam);
    355 
    356 
    357 
     364    SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0));
    358365
    359366    node1->attachObject(ent1);
     
    376383    ElementLocationArray[7] = node8->getPosition();
    377384    ElementLocationArray[8] = node9->getPosition();
    378 /*
    379 ElementLocationArray[5] = node6->getPosition();
    380 ElementLocationArray[6] = node7->getPosition();*/
     385
    381386    ElementSpeedArray[0] = (0,0,0);
    382387    ElementSpeedArray[1] = (0,0,0);
     
    388393    ElementSpeedArray[7] = (0,0,0);
    389394    ElementSpeedArray[8] = (0,0,0);
    390 /*
    391 ElementSpeedArray[5] = (0,0,0);
    392 ElementSpeedArray[6] = (0,0,0); */
     395
    393396    ElementAccelerationArray[0] = (0,0,0);
    394397    ElementAccelerationArray[1] = (0,0,0);
     
    400403    ElementAccelerationArray[7] = (0,0,0);
    401404    ElementAccelerationArray[8] = (0,0,0);
    402 /*
    403 ElementAccelerationArray[5] = (0,0,0);
    404 ElementAccelerationArray[6] = (0,0,0); */
     405
    405406    arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0], true);
    406407    arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1], true);
     
    412413    arrayOfElements[7].setValues( ElementLocationArray[7], ElementSpeedArray[7], ElementAccelerationArray[7], true);
    413414    arrayOfElements[8].setValues( ElementLocationArray[8], ElementSpeedArray[8], ElementAccelerationArray[8], false);
    414 /*
    415 arrayOfElements[5].setValues( ElementLocationArray[5], ElementSpeedArray[5], ElementAccelerationArray[5], false);
    416 arrayOfElements[6].setValues( ElementLocationArray[6], ElementSpeedArray[6], ElementAccelerationArray[6], false);*/
    417 
    418 
    419 
    420 
    421    /* for (int i=0; i<3; i++) {
    422       Element* arrayOfElements[i] = new Element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] );
    423     } */
    424    /* for (int i=0; i<3; i++) {
    425     arrayOfElements[i]->update(arrayOfElements);
    426     }  */
    427 
    428 //testing AIPilot -> function steer
    429   //  AIPilot temp;
    430   //  Vector3 foo = temp.steer(Vector3(0,0,1));
     415
     416   // END TEST DATA FLOCKING
    431417
    432418
Note: See TracChangeset for help on using the changeset viewer.