Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added actual flocking code and two frameworks

File size: 6.1 KB
Line 
1//
2//
3//      TODO: testing orxonox -flocking interface
4//            testing algorithm
5
6// ueberpruefen ob vektoren relativ richtig berechnet werden
7//
8//My Flocking Class
9
10#ifndef Flocking_Class
11#define Flocking_Class
12
13#include <Ogre.h>
14#include <OgreVector3.h>
15
16
17#include <iostream>
18
19
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
32    bool movable;  // movability of the element
33
34  Element() {
35    acceleration = (0,0,0);
36    speed = (0,0,0);
37    location = (0,0,0);
38    movable = true;
39  }
40
41  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
42    acceleration = acceleration_;
43    speed = speed_;
44    location = location_;
45    movable = movable_;
46  }
47
48  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
49    acceleration = acceleration_;
50    speed = speed_;
51    location = location_;
52    movable = movable_;
53  }
54
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
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    }   */
70  }
71
72//EINFÜGEN DES ELEMENTS
73  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);
77  }
78
79  void calculateSpeed(const FrameEvent& time) {
80    speed = speed + acceleration*time.timeSinceLastFrame;
81  }
82
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);
91    int numberOfNeighbour = 0;  //number of observed neighbours
92    float distance = 0;
93    //go through all elements
94    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
95      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
108        numberOfNeighbour++;  //counts the elements inside the detectionradius
109      }
110    }
111    if(numberOfNeighbour > 0) {
112    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
113    }
114    cout<<*steering<<endl;
115    return *steering;
116  }
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
Note: See TracBrowser for help on using the repository browser.