Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added Flocking

File size: 5.3 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#include <iostream>
17
18
19#endif
20
21using namespace std;
22using namespace Ogre;
23
24class Element // An element that flocks
25{
26
27  public:
28    Vector3 location;  // locationvector of the element
29    Vector3 speed;  // speedvector of the element
30    Vector3 acceleration;  // accelerationvector of the element
31
32  Element() {
33    acceleration = (0,0,0);
34    speed = (0,0,0);
35    location = (0,0,0);
36  }
37
38  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
39    acceleration = acceleration_;
40    speed = speed_;
41    location = location_;
42  }
43
44  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
45    acceleration = acceleration_;
46    speed = speed_;
47    location = location_;
48  }
49
50  //calculates the distance between the element and an other point given by temp
51  float getDistance(Element temp) {
52    Vector3 distance = temp.location-location;  //this doesn't work
53    return distance.length();
54  }
55
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  }
62
63//EINF[GEN DES ELEMENTS
64  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  }
69
70  void calculateSpeed(const FrameEvent& time) {
71    speed = speed + acceleration*time.timeSinceLastFrame;
72  }
73
74  void calculateLocation(const FrameEvent& time) {
75    location = location + speed*time.timeSinceLastFrame;
76  }
77
78
79  Vector3 separation(Element arrayOfElements[]) {
80    Vector3* steering = new Vector3(0,0,0); //steeringvector
81    int numberOfNeighbour = 0;  //number of observed neighbours
82    //go through all elements
83    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
84      Element actual = arrayOfElements[i];  //get the actual element
85      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
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 -> separation steeringvector
97    }
98    // cout << *steering << endl;
99    return *steering;
100  }
101
102  Vector3 alignment(Element arrayOfElements[]) {
103    Vector3* steering = new Vector3(0,0,0); //steeringvector
104    int numberOfNeighbour = 0;  //number of observed neighbours
105    //go through all elements
106    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
107      Element actual = arrayOfElements[i];  //get the actual element
108      float distance = getDistance(actual);  //get distance between this and actual
109//DUMMY ALIGNMENT DETECTION DISTANCE = 1000
110      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
111        *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
112        numberOfNeighbour++;  //counts the elements inside the detectionradius
113      }
114    }
115    if(numberOfNeighbour > 0) {
116    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
117    }
118    cout << *steering << endl;
119    return *steering;
120  }
121
122  Vector3 cohesion(Element arrayOfElements[]) {
123    Vector3* steering = new Vector3(0,0,0); //steeringvector
124    int numberOfNeighbour = 0;  //number of observed neighbours
125    //go through all elements
126    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
127      Element actual = arrayOfElements[i];  //get the actual element
128      float distance = getDistance(actual);  //get distance between this and actual
129// DUMMY COHESION DETECTION DISTANCE = 1000
130      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
131        *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
132        numberOfNeighbour++;  //counts the elements inside the detectionradius
133      }
134     }
135    if(numberOfNeighbour > 0) {
136    *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
137    }
138    return *steering;
139  }
140 
141};
142
143
144
145//End of My Flocking Class
Note: See TracBrowser for help on using the repository browser.