Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/Flocking.h @ 668

Last change on this file since 668 was 618, checked in by nicolasc, 17 years ago
  • changed comments to doxygen tags in flocking
  • reduced ogre depency in HUD and ParticleInterface
  • various
File size: 6.4 KB
Line 
1
2//Headerfile: Flocking.h
3
4#ifndef Flocking_Class
5#define Flocking_Class
6
7// #include <Ogre.h>
8#include <OgreVector3.h>
9
10
11#include <iostream>
12
13
14#endif
15
16
17class Element // An element that flocks
18{
19
20  public:
21    Ogre::Vector3 location;                       //!< locationvector of the element
22    Ogre::Vector3 speed;                          //!< speedvector of the element
23    Ogre::Vector3 acceleration;                   //!< accelerationvector of the element
24    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
25    static int const SEPERATIONDISTANCE = 300;    //!< detectionradius of seperation
26    static int const ALIGNMENTDISTANCE = 300;     //!< detectionradius of alignment
27    static int const COHESIONDISTANCE = 5000;     //!< detectionradius of cohesion
28    static int const ANZELEMENTS = 9;             //!< number of elements
29
30  //! default constructor
31  Element() {
32    acceleration = Ogre::Vector3(0,0,0);
33    speed = Ogre::Vector3(0,0,0);
34    location = Ogre::Vector3(0,0,0);
35    movable = true;
36  }
37
38  /** constructor
39   *  @param location_ sets locationvector of the element
40   *  @param speed_ sets speedvector of the element
41   *  @param acceleration_ sets accelerationvector of the element
42   *  @param movable_ sets movability of the element
43   */
44  Element(Ogre::Vector3 location_, Ogre::Vector3 speed_, Ogre::Vector3 acceleration_, bool movable_) {
45    acceleration = acceleration_;
46    speed = speed_;
47    location = location_;
48    movable = movable_;
49  }
50
51  //! function to chance values of an element
52  void setValues(Ogre::Vector3 location_, Ogre::Vector3 speed_, Ogre::Vector3 acceleration_, bool movable_) {
53    acceleration = acceleration_;
54    speed = speed_;
55    location = location_;
56    movable = movable_;
57  }
58
59  /** calculates the distance between the element and an other point given by temp
60   * @param e remote object to calculate distance to
61   */
62  float getDistance(Element e) {
63    Ogre::Vector3 distance = e.location - location;
64    return distance.length();
65  }
66
67  //! updates the data of an element
68  void update(Element arrayOfElements[]) {
69    if (this->movable == true) {calculateAcceleration(arrayOfElements);} //if element is movable, calculate acceleration
70  }
71
72  //! calculates the new acceleration of an element
73  void calculateAcceleration(Element arrayOfElements[]) {
74    acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);  //acceleration consisting of flocking-functions
75  }
76
77  //! separation-function (keep elements separated, avoid crashs)
78  Ogre::Vector3 separation(Element arrayOfElements[]) {
79    using namespace Ogre;
80    Vector3 steering = Vector3(0,0,0); //steeringvector
81    Vector3 inverseDistance = Vector3(0,0,0);  //vector pointing away from possible collisions
82    int numberOfNeighbour = 0;  //number of observed neighbours
83    float distance = 0;  // distance to the actual element
84    for(int i=0; i<ANZELEMENTS; i++) {  //go through all elements
85      Element actual = arrayOfElements[i];  //get the actual element
86      distance = getDistance(actual);  //get distance between this and actual
87      if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
88        inverseDistance = (0,0,0);
89        inverseDistance = location-actual.location;  //calculate the distancevector heading towards this
90        //adaptation of the inverseDistance to the distance
91        if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
92        if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;}
93        if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;}
94        if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;}
95        steering = steering + inverseDistance;  //add up all significant steeringvectors
96        numberOfNeighbour++;  //counts the elements inside the detectionradius
97      }
98    }
99    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
100    return steering;
101  }
102
103  //! alignment-function (lead elements to the same heading)
104  Ogre::Vector3 alignment(Element arrayOfElements[]) {
105    using namespace Ogre;
106    Vector3 steering = Vector3(0,0,0); //steeringvector
107    int numberOfNeighbour = 0;  //number of observed neighbours
108    float distance = 0;
109    //go through all elements
110    for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
111      Element actual = arrayOfElements[i];  //get the actual element
112      float distance = getDistance(actual);  //get distance between this and actual
113      if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
114        steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
115        numberOfNeighbour++;  //counts the elements inside the detectionradius
116      }
117    }
118    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
119    return steering;
120  }
121
122  //! cohseion-function (keep elements close to each other)
123  Ogre::Vector3 cohesion(Element arrayOfElements[]) {
124    using namespace Ogre;
125    Vector3 steering = Vector3(0,0,0); //steeringvector
126    int numberOfNeighbour = 0;  //number of observed neighbours
127    float distance = 0;
128    //go through all elements
129    for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
130      Element actual = arrayOfElements[i];  //get the actual element
131      float distance = getDistance(actual);  //get distance between this and actual
132      if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
133        steering = steering + actual.location;  //add up all locations of elements inside the detectionradius
134        numberOfNeighbour++;  //counts the elements inside the detectionradius
135      }
136    }
137    if(numberOfNeighbour > 0) {
138      steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
139      steering = steering - this->location;  //transform the vector for the ship
140    }
141    return steering;
142  }
143};     //End of class Element
Note: See TracBrowser for help on using the repository browser.