Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Headerfile which contains the flocking algorithm

File size: 4.5 KB
Line 
1
2//My Flocking Class
3
4#ifndef Flocking_Class
5#define Flocking_Class
6
7#include <Ogre.h>
8#include <OgreVector3.h>
9
10
11#endif
12
13using namespace std;
14using namespace Ogre;
15
16class Element // An element that flocks
17{
18
19  public:
20    Vector3 location;  // locationvector of the element
21    Vector3 speed;  // speedvector of the element
22    Vector3 acceleration;  // accelerationvector of the element
23
24
25  void element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
26    acceleration = acceleration_;
27    speed = speed_;
28    location = location_;
29  }
30
31  //calculates the distance between the element and an other point given by temp
32  float getDistance(Element temp) {
33    Vector3 distance = temp.location-location;  //this doesn't work
34    return distance.length();
35  }
36
37//EINF[GEN DES ELEMENTS
38  void update(/*Element übergeben*/) {
39    calculateAcceleration(/*Elementübergabe*/);  //updates the acceleration
40    calculateSpeed();  //updates the speed
41    calculateLocation();  //updates the location
42  }
43
44//EINF[GEN DES ELEMENTS
45  void calculateAcceleration(/*Element übergeben*/) {
46  //calculates the accelerationvector based on the steeringvectors of
47  //separtion, alignment and cohesion.
48  acceleration = acceleration + separation() + alignment() + cohesion();
49  }
50
51  void calculateSpeed() {
52  speed = speed + acceleration;
53  }
54
55  void calculateLocation() {
56  location = location + speed;
57  acceleration = (0,0,0);  //set acceleration to zero for the next calculation
58  }
59
60  Vector3 separation() {
61    Vector3 steering; //steeringvector
62    int numberOfNeighbour;  //number of observed neighbours
63    //go through all elements
64    for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
65      Element actual = arrayOfElements[i];  //get the actual element
66      float distance = Element.getDistance(actual);  //get distance between this and actual
67//DUMMY SEPERATION DETECTION DISTANCE = 25
68      if ((distance > 0) && (distance<25)) {  //do only if actual is inside detectionradius
69        Vector3 inverseDistance = actual.location-location;  //calculate the distancevector heading towards this
70        inverseDistance = inverseDistance.normalise(); //does this work correctly?  //normalise the distancevector
71        inverseDistance = inverseDistance/distance;  //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)
72        steering = steering + inverseDistance;  //add up all significant steeringvectors
73        numberOfNeighbour++;  //counts the elements inside the detectionradius
74      }
75    }
76    if(numberOfNeighbour > 0) {
77    steering = steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
78    }
79    return steering;
80  }
81
82  Vector3 alignment() {
83    Vector3 steering; //steeringvector
84    int numberOfNeighbour;  //number of observed neighbours
85    //go through all elements
86    for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
87      Element actual = arrayOfElements[i];  //get the actual element
88      float distance = Element.getDistance(actual);  //get distance between this and actual
89//DUMMY ALIGNMENT DETECTION DISTANCE = 50
90      if ((distance > 0) && (distance<50)) {  //check if actual element is inside detectionradius
91        steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
92        numberOfNeighbour++;  //counts the elements inside the detectionradius
93      }
94    }
95    if(numberOfNeighbour > 0) {
96    steering = steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
97    }
98    return steering;
99  }
100
101  Vector3 cohesion() {
102    Vector3 steering; //steeringvector
103    int numberOfNeighbour;  //number of observed neighbours
104    //go through all elements
105    for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
106      Element actual = arrayOfElements[i];  //get the actual element
107      float distance = Element.getDistance(actual);  //get distance between this and actual
108// DUMMY COHESION DETECTION DISTANCE = 50
109      if ((distance > 0) && (distance<50)) {  //check if actual element is inside detectionradius
110        steering = steering + actual.location;  //add up all locations of elements 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 steeringvector by the number of elements -> cohesion steeringvector
116    }
117  }
118
119};
120
121
122
123//End of My Flocking Class
Note: See TracBrowser for help on using the repository browser.