| 1 | |
|---|
| 2 | //My Flocking Class |
|---|
| 3 | |
|---|
| 4 | #ifndef Flocking_Class |
|---|
| 5 | #define Flocking_Class |
|---|
| 6 | |
|---|
| 7 | #include <Ogre.h> |
|---|
| 8 | #include <OgreVector3.h> |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | #endif |
|---|
| 12 | |
|---|
| 13 | using namespace std; |
|---|
| 14 | using namespace Ogre; |
|---|
| 15 | |
|---|
| 16 | class Element // An element that flocks |
|---|
| 17 | { |
|---|
| 18 | |
|---|
| 19 | public: |
|---|
| 20 | Vector3 location; // locationvector of the element |
|---|
| 21 | Vector3 speed; // speedvector of the element |
|---|
| 22 | Vector3 acceleration; // accelerationvector of the element |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) { |
|---|
| 26 | acceleration = acceleration_; |
|---|
| 27 | speed = speed_; |
|---|
| 28 | location = location_; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | //calculates the distance between the element and an other point given by temp |
|---|
| 32 | float getDistance(Element temp) { |
|---|
| 33 | Vector3 distance = temp.location-location; //this doesn't work |
|---|
| 34 | return distance.length(); |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | //EINF[GEN DES ELEMENTS |
|---|
| 38 | void update(Element* arrayOfElements) { |
|---|
| 39 | calculateAcceleration(arrayOfElements); //updates the acceleration |
|---|
| 40 | calculateSpeed(); //updates the speed |
|---|
| 41 | calculateLocation(); //updates the location |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | //EINF[GEN DES ELEMENTS |
|---|
| 45 | void calculateAcceleration(Element* arrayOfElements) { |
|---|
| 46 | //calculates the accelerationvector based on the steeringvectors of |
|---|
| 47 | //separtion, alignment and cohesion. |
|---|
| 48 | acceleration = acceleration + separation(arrayOfElements) + 2*alignment(arrayOfElements) + 2*cohesion(arrayOfElements); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void calculateSpeed() { |
|---|
| 52 | speed = speed + acceleration; |
|---|
| 53 | //speed = speed.normalise(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void calculateLocation() { |
|---|
| 57 | location = location + speed; |
|---|
| 58 | acceleration = (0,0,0); //set acceleration to zero for the next calculation |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | Vector3 separation(Element* arrayOfElements) { |
|---|
| 62 | Vector3 steering; //steeringvector |
|---|
| 63 | int numberOfNeighbour; //number of observed neighbours |
|---|
| 64 | //go through all elements |
|---|
| 65 | for(int i=1; i<3; i++) { //just working with 3 elements at the moment |
|---|
| 66 | Element actual = arrayOfElements[i]; //get the actual element |
|---|
| 67 | float distance = getDistance(actual); //get distance between this and actual |
|---|
| 68 | //DUMMY SEPERATION DETECTION DISTANCE = 25 |
|---|
| 69 | if ((distance > 0) && (distance<1)) { //do only if actual is inside detectionradius |
|---|
| 70 | Vector3 inverseDistance = actual.location-location; //calculate the distancevector heading towards this |
|---|
| 71 | inverseDistance = inverseDistance.normalise(); //does this work correctly? //normalise the distancevector |
|---|
| 72 | inverseDistance = inverseDistance/*/distance;*/ ; //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector) |
|---|
| 73 | steering = steering + inverseDistance; //add up all significant steeringvectors |
|---|
| 74 | numberOfNeighbour++; //counts the elements inside the detectionradius |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | if(numberOfNeighbour > 0) { |
|---|
| 78 | steering = steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> separation steeringvector |
|---|
| 79 | } |
|---|
| 80 | return steering; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | Vector3 alignment(Element* arrayOfElements) { |
|---|
| 84 | Vector3 steering; //steeringvector |
|---|
| 85 | int numberOfNeighbour; //number of observed neighbours |
|---|
| 86 | //go through all elements |
|---|
| 87 | for(int i=1; i<3; i++) { //just working with 3 elements at the moment |
|---|
| 88 | Element actual = arrayOfElements[i]; //get the actual element |
|---|
| 89 | float distance = getDistance(actual); //get distance between this and actual |
|---|
| 90 | //DUMMY ALIGNMENT DETECTION DISTANCE = 50 |
|---|
| 91 | if ((distance > 0) && (distance<1000)) { //check if actual element is inside detectionradius |
|---|
| 92 | steering = steering + actual.speed; //add up all speedvectors inside the detectionradius |
|---|
| 93 | numberOfNeighbour++; //counts the elements inside the detectionradius |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | if(numberOfNeighbour > 0) { |
|---|
| 97 | steering = steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> alignment steeringvector |
|---|
| 98 | } |
|---|
| 99 | return steering; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | Vector3 cohesion(Element* arrayOfElements) { |
|---|
| 103 | Vector3 steering; //steeringvector |
|---|
| 104 | int numberOfNeighbour; //number of observed neighbours |
|---|
| 105 | //go through all elements |
|---|
| 106 | for(int i=1; i<3; i++) { //just working with 3 elements at the moment |
|---|
| 107 | Element actual = arrayOfElements[i]; //get the actual element |
|---|
| 108 | float distance = getDistance(actual); //get distance between this and actual |
|---|
| 109 | // DUMMY COHESION DETECTION DISTANCE = 50 |
|---|
| 110 | if ((distance > 0) && (distance<1000)) { //check if actual element is inside detectionradius |
|---|
| 111 | steering = steering + actual.location; //add up all locations of elements inside the detectionradius |
|---|
| 112 | numberOfNeighbour++; //counts the elements inside the detectionradius |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | if(numberOfNeighbour > 0) { |
|---|
| 116 | steering = steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | //End of My Flocking Class |
|---|