Changeset 708 for code/branches/FICN/src/orxonox/Flocking.h
- Timestamp:
- Dec 28, 2007, 10:30:29 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/orxonox/Flocking.h
r673 r708 5 5 #define _Flocking_H__ 6 6 7 // #include <Ogre.h> 8 #include <OgreVector3.h> 7 #include "misc/Vector3.h" 9 8 10 11 #include <iostream> 12 13 14 15 class Element // An element that flocks 9 namespace orxonox 16 10 { 11 class Element // An element that flocks 12 { 17 13 18 14 public: 19 Ogre::Vector3 location; //!< locationvector of the element20 Ogre::Vector3 speed; //!< speedvector of the element21 Ogre::Vector3 acceleration; //!< accelerationvector of the element15 Vector3 location; //!< locationvector of the element 16 Vector3 speed; //!< speedvector of the element 17 Vector3 acceleration; //!< accelerationvector of the element 22 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 23 19 static int const SEPERATIONDISTANCE = 300; //!< detectionradius of seperation … … 26 22 static int const ANZELEMENTS = 9; //!< number of elements 27 23 28 //! default constructor29 Element() {30 acceleration = Ogre::Vector3(0,0,0);31 speed = Ogre::Vector3(0,0,0);32 location = Ogre::Vector3(0,0,0);33 movable = true;34 }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 } 35 31 36 /** constructor37 * @param location_ sets locationvector of the element38 * @param speed_ sets speedvector of the element39 * @param acceleration_ sets accelerationvector of the element40 * @param movable_ sets movability of the element41 */42 Element(Ogre::Vector3 location_, Ogre::Vector3 speed_, Ogre::Vector3 acceleration_, bool movable_) {43 acceleration = acceleration_;44 speed = speed_;45 location = location_;46 movable = movable_;47 }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 } 48 44 49 //! function to chance values of an element50 void setValues(Ogre::Vector3 location_, Ogre::Vector3 speed_, Ogre::Vector3 acceleration_, bool movable_) {51 acceleration = acceleration_;52 speed = speed_;53 location = location_;54 movable = movable_;55 }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 } 56 52 57 /** calculates the distance between the element and an other point given by temp58 * @param e remote object to calculate distance to59 */60 float getDistance(Element e) {61 Ogre::Vector3 distance = e.location - location;62 return distance.length();63 }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 } 64 60 65 //! updates the data of an element66 void update(Element arrayOfElements[]) {67 if (this->movable == true) {calculateAcceleration(arrayOfElements);} //if element is movable, calculate acceleration68 }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 } 69 65 70 //! calculates the new acceleration of an element71 void calculateAcceleration(Element arrayOfElements[]) {72 acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements); //acceleration consisting of flocking-functions73 }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 } 74 70 75 //! separation-function (keep elements separated, avoid crashs) 76 Ogre::Vector3 separation(Element arrayOfElements[]) { 77 using namespace Ogre; 78 Vector3 steering = Vector3(0,0,0); //steeringvector 79 Vector3 inverseDistance = Vector3(0,0,0); //vector pointing away from possible collisions 80 int numberOfNeighbour = 0; //number of observed neighbours 81 float distance = 0; // distance to the actual element 82 for(int i=0; i<ANZELEMENTS; i++) { //go through all elements 83 Element actual = arrayOfElements[i]; //get the actual element 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 94 numberOfNeighbour++; //counts the elements inside the detectionradius 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 } 95 92 } 93 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> separation steeringvector 94 return steering; 96 95 } 97 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> separation steeringvector98 return steering;99 }100 96 101 //! alignment-function (lead elements to the same heading) 102 Ogre::Vector3 alignment(Element arrayOfElements[]) { 103 using namespace Ogre; 104 Vector3 steering = Vector3(0,0,0); //steeringvector 105 int numberOfNeighbour = 0; //number of observed neighbours 106 float distance = 0; 107 //go through all elements 108 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 109 Element actual = arrayOfElements[i]; //get the actual element 110 float distance = getDistance(actual); //get distance between this and actual 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 113 numberOfNeighbour++; //counts the elements inside the detectionradius 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 } 114 111 } 112 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> alignment steeringvector 113 return steering; 115 114 } 116 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> alignment steeringvector117 return steering;118 }119 115 120 //! cohseion-function (keep elements close to each other) 121 Ogre::Vector3 cohesion(Element arrayOfElements[]) { 122 using namespace Ogre; 123 Vector3 steering = Vector3(0,0,0); //steeringvector 124 int numberOfNeighbour = 0; //number of observed neighbours 125 float distance = 0; 126 //go through all elements 127 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 128 Element actual = arrayOfElements[i]; //get the actual element 129 float distance = getDistance(actual); //get distance between this and actual 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 132 numberOfNeighbour++; //counts the elements inside the detectionradius 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 } 133 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; 134 136 } 135 if(numberOfNeighbour > 0) { 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 138 } 139 return steering; 140 } 141 }; //End of class Element 137 }; //End of class Element 138 } 142 139 143 140 #endif /* _Flocking_H__*/
Note: See TracChangeset
for help on using the changeset viewer.