[212] | 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 | |
---|
[233] | 25 | Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) { |
---|
[212] | 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 |
---|
[233] | 38 | void update(Element* arrayOfElements) { |
---|
| 39 | calculateAcceleration(arrayOfElements); //updates the acceleration |
---|
[212] | 40 | calculateSpeed(); //updates the speed |
---|
| 41 | calculateLocation(); //updates the location |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | //EINF[GEN DES ELEMENTS |
---|
[233] | 45 | void calculateAcceleration(Element* arrayOfElements) { |
---|
[212] | 46 | //calculates the accelerationvector based on the steeringvectors of |
---|
| 47 | //separtion, alignment and cohesion. |
---|
[233] | 48 | acceleration = acceleration + separation(arrayOfElements) + 2*alignment(arrayOfElements) + 2*cohesion(arrayOfElements); |
---|
[212] | 49 | } |
---|
| 50 | |
---|
| 51 | void calculateSpeed() { |
---|
| 52 | speed = speed + acceleration; |
---|
[233] | 53 | //speed = speed.normalise(); |
---|
[212] | 54 | } |
---|
| 55 | |
---|
| 56 | void calculateLocation() { |
---|
| 57 | location = location + speed; |
---|
| 58 | acceleration = (0,0,0); //set acceleration to zero for the next calculation |
---|
| 59 | } |
---|
| 60 | |
---|
[233] | 61 | Vector3 separation(Element* arrayOfElements) { |
---|
[212] | 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 |
---|
[233] | 67 | float distance = getDistance(actual); //get distance between this and actual |
---|
[212] | 68 | //DUMMY SEPERATION DETECTION DISTANCE = 25 |
---|
[233] | 69 | if ((distance > 0) && (distance<1)) { //do only if actual is inside detectionradius |
---|
[212] | 70 | Vector3 inverseDistance = actual.location-location; //calculate the distancevector heading towards this |
---|
| 71 | inverseDistance = inverseDistance.normalise(); //does this work correctly? //normalise the distancevector |
---|
[233] | 72 | inverseDistance = inverseDistance/*/distance;*/ ; //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector) |
---|
[212] | 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 | |
---|
[233] | 83 | Vector3 alignment(Element* arrayOfElements) { |
---|
[212] | 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 |
---|
[233] | 89 | float distance = getDistance(actual); //get distance between this and actual |
---|
[212] | 90 | //DUMMY ALIGNMENT DETECTION DISTANCE = 50 |
---|
[233] | 91 | if ((distance > 0) && (distance<1000)) { //check if actual element is inside detectionradius |
---|
[212] | 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 | |
---|
[233] | 102 | Vector3 cohesion(Element* arrayOfElements) { |
---|
[212] | 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 |
---|
[233] | 108 | float distance = getDistance(actual); //get distance between this and actual |
---|
[212] | 109 | // DUMMY COHESION DETECTION DISTANCE = 50 |
---|
[233] | 110 | if ((distance > 0) && (distance<1000)) { //check if actual element is inside detectionradius |
---|
[212] | 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 |
---|