| [342] | 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 |  | 
|---|
 | 21 | using namespace std; | 
|---|
 | 22 | using namespace Ogre; | 
|---|
 | 23 |  | 
|---|
 | 24 | class 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 |     return *steering; | 
|---|
 | 99 |   } | 
|---|
 | 100 |  | 
|---|
 | 101 |   Vector3 alignment(Element arrayOfElements[]) { | 
|---|
 | 102 |     Vector3* steering = new Vector3(0,0,0); //steeringvector | 
|---|
 | 103 |     int numberOfNeighbour = 0;  //number of observed neighbours | 
|---|
 | 104 |     //go through all elements | 
|---|
 | 105 |     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment | 
|---|
 | 106 |       Element actual = arrayOfElements[i];  //get the actual element | 
|---|
 | 107 |       float distance = getDistance(actual);  //get distance between this and actual | 
|---|
 | 108 | //DUMMY ALIGNMENT DETECTION DISTANCE = 1000 | 
|---|
 | 109 |       if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius | 
|---|
 | 110 |         *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius | 
|---|
 | 111 |         numberOfNeighbour++;  //counts the elements inside the detectionradius | 
|---|
 | 112 |       } | 
|---|
 | 113 |     } | 
|---|
 | 114 |     if(numberOfNeighbour > 0) { | 
|---|
 | 115 |     *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector | 
|---|
 | 116 |     } | 
|---|
 | 117 |     return *steering; | 
|---|
 | 118 |   } | 
|---|
 | 119 |  | 
|---|
 | 120 |   Vector3 cohesion(Element arrayOfElements[]) { | 
|---|
 | 121 |     Vector3* steering = new Vector3(0,0,0); //steeringvector | 
|---|
 | 122 |     int numberOfNeighbour = 0;  //number of observed neighbours | 
|---|
 | 123 |     //go through all elements | 
|---|
 | 124 |     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment | 
|---|
 | 125 |       Element actual = arrayOfElements[i];  //get the actual element | 
|---|
 | 126 |       float distance = getDistance(actual);  //get distance between this and actual | 
|---|
 | 127 | // DUMMY COHESION DETECTION DISTANCE = 1000 | 
|---|
 | 128 |       if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius | 
|---|
 | 129 |         *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius | 
|---|
 | 130 |         numberOfNeighbour++;  //counts the elements inside the detectionradius | 
|---|
 | 131 |       } | 
|---|
 | 132 |      } | 
|---|
 | 133 |     if(numberOfNeighbour > 0) { | 
|---|
 | 134 |     *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector | 
|---|
 | 135 |     } | 
|---|
 | 136 |     return *steering; | 
|---|
 | 137 |   } | 
|---|
 | 138 |   | 
|---|
 | 139 | }; | 
|---|
 | 140 |  | 
|---|
 | 141 |  | 
|---|
 | 142 |  | 
|---|
 | 143 | //End of My Flocking Class | 
|---|