Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI/src/Flocking.h @ 426

Last change on this file since 426 was 426, checked in by motth, 16 years ago

added actual flocking code and two frameworks

File size: 6.1 KB
RevLine 
[325]1//
2//
3//      TODO: testing orxonox -flocking interface
4//            testing algorithm
[212]5
[325]6// ueberpruefen ob vektoren relativ richtig berechnet werden
7//
[212]8//My Flocking Class
9
10#ifndef Flocking_Class
11#define Flocking_Class
12
13#include <Ogre.h>
14#include <OgreVector3.h>
15
[426]16
[325]17#include <iostream>
[212]18
[325]19
[212]20#endif
21
22using namespace std;
23using namespace Ogre;
24
25class Element // An element that flocks
26{
27
28  public:
29    Vector3 location;  // locationvector of the element
30    Vector3 speed;  // speedvector of the element
31    Vector3 acceleration;  // accelerationvector of the element
[426]32    bool movable;  // movability of the element
[212]33
[325]34  Element() {
35    acceleration = (0,0,0);
36    speed = (0,0,0);
37    location = (0,0,0);
[426]38    movable = true;
[325]39  }
[212]40
[426]41  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
[212]42    acceleration = acceleration_;
43    speed = speed_;
44    location = location_;
[426]45    movable = movable_;
[212]46  }
47
[426]48  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
[325]49    acceleration = acceleration_;
50    speed = speed_;
51    location = location_;
[426]52    movable = movable_;
[325]53  }
54
[212]55  //calculates the distance between the element and an other point given by temp
56  float getDistance(Element temp) {
57    Vector3 distance = temp.location-location;  //this doesn't work
58    return distance.length();
59  }
60
[426]61//EINFÜGEN DES ELEMENTS
[325]62  void update(Element arrayOfElements[], const FrameEvent& time) {
[426]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    }   */
[212]70  }
71
[426]72//EINFÜGEN DES ELEMENTS
[325]73  void calculateAcceleration(Element arrayOfElements[]) {
[212]74  //calculates the accelerationvector based on the steeringvectors of
75  //separtion, alignment and cohesion.
[325]76  acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);
[212]77  }
78
[325]79  void calculateSpeed(const FrameEvent& time) {
80    speed = speed + acceleration*time.timeSinceLastFrame;
[212]81  }
82
[325]83  void calculateLocation(const FrameEvent& time) {
84    location = location + speed*time.timeSinceLastFrame;
[212]85  }
86
[325]87
88  Vector3 separation(Element arrayOfElements[]) {
89    Vector3* steering = new Vector3(0,0,0); //steeringvector
[426]90    Vector3* inverseDistance = new Vector3(0,0,0);
[325]91    int numberOfNeighbour = 0;  //number of observed neighbours
[426]92    float distance = 0;
[212]93    //go through all elements
[426]94    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
[212]95      Element actual = arrayOfElements[i];  //get the actual element
[426]96      distance = getDistance(actual);  //get distance between this and actual
[325]97//DUMMY SEPERATION DETECTION DISTANCE =100
[426]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
[212]108        numberOfNeighbour++;  //counts the elements inside the detectionradius
109      }
110    }
111    if(numberOfNeighbour > 0) {
[325]112    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
[212]113    }
[426]114    cout<<*steering<<endl;
[325]115    return *steering;
[212]116  }
117
[325]118  Vector3 alignment(Element arrayOfElements[]) {
119    Vector3* steering = new Vector3(0,0,0); //steeringvector
120    int numberOfNeighbour = 0;  //number of observed neighbours
[426]121    float distance = 0;
[212]122    //go through all elements
[426]123    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
[212]124      Element actual = arrayOfElements[i];  //get the actual element
[233]125      float distance = getDistance(actual);  //get distance between this and actual
[325]126//DUMMY ALIGNMENT DETECTION DISTANCE = 1000
[426]127      if ((distance > 0) && (distance < 300)) {  //check if actual element is inside detectionradius
[325]128        *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
[212]129        numberOfNeighbour++;  //counts the elements inside the detectionradius
130      }
131    }
132    if(numberOfNeighbour > 0) {
[325]133    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
[212]134    }
[325]135    return *steering;
[212]136  }
137
[325]138  Vector3 cohesion(Element arrayOfElements[]) {
139    Vector3* steering = new Vector3(0,0,0); //steeringvector
140    int numberOfNeighbour = 0;  //number of observed neighbours
[426]141    float distance = 0;
[212]142    //go through all elements
[426]143    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
[212]144      Element actual = arrayOfElements[i];  //get the actual element
[233]145      float distance = getDistance(actual);  //get distance between this and actual
[325]146// DUMMY COHESION DETECTION DISTANCE = 1000
[426]147      if ((distance > 0) && (distance < 5000)) {  //check if actual element is inside detectionradius
[325]148        *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
[212]149        numberOfNeighbour++;  //counts the elements inside the detectionradius
150      }
151     }
152    if(numberOfNeighbour > 0) {
[325]153    *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
[426]154    *steering = *steering - this->location;  // (?) Koordinatensystem?
[212]155    }
[325]156    return *steering;
[212]157  }
158};
159
160
161
162//End of My Flocking Class
Note: See TracBrowser for help on using the repository browser.