Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 17, 2007, 9:50:55 PM (17 years ago)
Author:
bknecht
Message:

added/updated AI-files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/orxonox/Flocking.h

    r342 r596  
    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
     
    148#include <OgreVector3.h>
    159
     10
    1611#include <iostream>
    1712
     
    1914#endif
    2015
    21 using namespace std;
    2216using namespace Ogre;
    2317
     
    2923    Vector3 speed;  // speedvector of the element
    3024    Vector3 acceleration;  // accelerationvector 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
    3130
     31  //default constructor
    3232  Element() {
    3333    acceleration = (0,0,0);
    3434    speed = (0,0,0);
    3535    location = (0,0,0);
     36    movable = true;
    3637  }
    3738
    38   Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
     39  //constructor
     40  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
    3941    acceleration = acceleration_;
    4042    speed = speed_;
    4143    location = location_;
     44    movable = movable_;
    4245  }
    4346
    44   void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
     47  //function to chance values of an element
     48  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
    4549    acceleration = acceleration_;
    4650    speed = speed_;
    4751    location = location_;
     52    movable = movable_;
    4853  }
    4954
    5055  //calculates the distance between the element and an other point given by temp
    5156  float getDistance(Element temp) {
    52     Vector3 distance = temp.location-location;  //this doesn't work
     57    Vector3 distance = temp.location-location;
    5358    return distance.length();
    5459  }
    5560
    56 //EINF[GEN DES ELEMENTS
    57   void update(Element arrayOfElements[], const FrameEvent& time) {
    58     calculateAcceleration(arrayOfElements);  //updates the acceleration
    59     calculateSpeed(time);  //updates the speed
    60     calculateLocation(time);  //updates the location
     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
    6164  }
    6265
    63 //EINF[GEN DES ELEMENTS
     66  //calculates the new acceleration of an element
    6467  void calculateAcceleration(Element arrayOfElements[]) {
    65   //calculates the accelerationvector based on the steeringvectors of
    66   //separtion, alignment and cohesion.
    67   acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);
     68    acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);  //acceleration consisting of flocking-functions
    6869  }
    6970
    70   void calculateSpeed(const FrameEvent& time) {
    71     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;
    7294  }
    7395
    74   void calculateLocation(const FrameEvent& time) {
    75     location = location + speed*time.timeSinceLastFrame;
     96  //alignment-function (lead elements to the same heading)
     97  Vector3 alignment(Element arrayOfElements[]) {
     98    Vector3 steering = Vector3(0,0,0); //steeringvector
     99    int numberOfNeighbour = 0;  //number of observed neighbours
     100    float distance = 0;
     101    //go through all elements
     102    for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
     103      Element actual = arrayOfElements[i];  //get the actual element
     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;
    76112  }
    77113
    78 
    79   Vector3 separation(Element arrayOfElements[]) {
    80     Vector3* steering = new Vector3(0,0,0); //steeringvector
     114  //cohseion-function (keep elements close to each other)
     115  Vector3 cohesion(Element arrayOfElements[]) {
     116    Vector3 steering = Vector3(0,0,0); //steeringvector
    81117    int numberOfNeighbour = 0;  //number of observed neighbours
     118    float distance = 0;
    82119    //go through all elements
    83     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
     120    for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
    84121      Element actual = arrayOfElements[i];  //get the actual element
    85122      float distance = getDistance(actual);  //get distance between this and actual
    86 //DUMMY SEPERATION DETECTION DISTANCE =100
    87       if ((distance > 0) && (distance<100)) {  //do only if actual is inside detectionradius
    88         Vector3 inverseDistance = actual.location-location;  //calculate the distancevector heading towards this
    89         inverseDistance = inverseDistance.normalise(); //does this work correctly?  //normalise the distancevector
    90         inverseDistance = inverseDistance/*/distance*/;  //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)
    91         *steering = *steering + inverseDistance;  //add up all significant steeringvectors
     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
    92125        numberOfNeighbour++;  //counts the elements inside the detectionradius
    93126      }
    94127    }
    95128    if(numberOfNeighbour > 0) {
    96     *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
    97131    }
    98     return *steering;
     132    return steering;
    99133  }
    100 
    101   Vector3 alignment(Element arrayOfElements[]) {
    102     Vector3* steering = new Vector3(0,0,0); //steeringvector
    103     int numberOfNeighbour = 0;  //number of observed neighbours
    104     //go through all elements
    105     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
    106       Element actual = arrayOfElements[i];  //get the actual element
    107       float distance = getDistance(actual);  //get distance between this and actual
    108 //DUMMY ALIGNMENT DETECTION DISTANCE = 1000
    109       if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
    110         *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
    111         numberOfNeighbour++;  //counts the elements inside the detectionradius
    112       }
    113     }
    114     if(numberOfNeighbour > 0) {
    115     *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
    116     }
    117     return *steering;
    118   }
    119 
    120   Vector3 cohesion(Element arrayOfElements[]) {
    121     Vector3* steering = new Vector3(0,0,0); //steeringvector
    122     int numberOfNeighbour = 0;  //number of observed neighbours
    123     //go through all elements
    124     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
    125       Element actual = arrayOfElements[i];  //get the actual element
    126       float distance = getDistance(actual);  //get distance between this and actual
    127 // DUMMY COHESION DETECTION DISTANCE = 1000
    128       if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
    129         *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
    130         numberOfNeighbour++;  //counts the elements inside the detectionradius
    131       }
    132      }
    133     if(numberOfNeighbour > 0) {
    134     *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
    135     }
    136     return *steering;
    137   }
    138  
    139 };
    140 
    141 
    142 
    143 //End of My Flocking Class
     134};     //End of class Element
Note: See TracChangeset for help on using the changeset viewer.