Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/Flocking.h @ 1361

Last change on this file since 1361 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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