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