Changeset 596 for code/branches/FICN/src
- Timestamp:
- Dec 17, 2007, 9:50:55 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/orxonox/Flocking.h
r342 r596 1 //2 //3 // TODO: testing orxonox -flocking interface4 // testing algorithm5 1 6 // ueberpruefen ob vektoren relativ richtig berechnet werden 7 // 8 //My Flocking Class 2 //Headerfile: Flocking.h 9 3 10 4 #ifndef Flocking_Class … … 14 8 #include <OgreVector3.h> 15 9 10 16 11 #include <iostream> 17 12 … … 19 14 #endif 20 15 21 using namespace std;22 16 using namespace Ogre; 23 17 … … 29 23 Vector3 speed; // speedvector of the element 30 24 Vector3 acceleration; // accelerationvector of the element 25 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 26 static int const SEPERATIONDISTANCE = 300; //detectionradius of seperation 27 static int const ALIGNMENTDISTANCE = 300; //detectionradius of alignment 28 static int const COHESIONDISTANCE = 5000; //detectionradius of cohesion 29 static int const ANZELEMENTS = 9; //number of elements 31 30 31 //default constructor 32 32 Element() { 33 33 acceleration = (0,0,0); 34 34 speed = (0,0,0); 35 35 location = (0,0,0); 36 movable = true; 36 37 } 37 38 38 Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) { 39 //constructor 40 Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) { 39 41 acceleration = acceleration_; 40 42 speed = speed_; 41 43 location = location_; 44 movable = movable_; 42 45 } 43 46 44 void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_) { 47 //function to chance values of an element 48 void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) { 45 49 acceleration = acceleration_; 46 50 speed = speed_; 47 51 location = location_; 52 movable = movable_; 48 53 } 49 54 50 55 //calculates the distance between the element and an other point given by temp 51 56 float getDistance(Element temp) { 52 Vector3 distance = temp.location-location; //this doesn't work57 Vector3 distance = temp.location-location; 53 58 return distance.length(); 54 59 } 55 60 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 //updates the data of an element 62 void update(Element arrayOfElements[]) { 63 if (this->movable == true) {calculateAcceleration(arrayOfElements);} //if element is movable, calculate acceleration 61 64 } 62 65 63 //EINF[GEN DES ELEMENTS 66 //calculates the new acceleration of an element 64 67 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 acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements); //acceleration consisting of flocking-functions 68 69 } 69 70 70 void calculateSpeed(const FrameEvent& time) { 71 speed = speed + acceleration*time.timeSinceLastFrame; 71 //separation-function (keep elements separated, avoid crashs) 72 Vector3 separation(Element arrayOfElements[]) { 73 Vector3 steering = Vector3(0,0,0); //steeringvector 74 Vector3 inverseDistance = Vector3(0,0,0); //vector pointing away from possible collisions 75 int numberOfNeighbour = 0; //number of observed neighbours 76 float distance = 0; // distance to the actual element 77 for(int i=0; i<ANZELEMENTS; i++) { //go through all elements 78 Element actual = arrayOfElements[i]; //get the actual element 79 distance = getDistance(actual); //get distance between this and actual 80 if ((distance > 0) && (distance < SEPERATIONDISTANCE)) { //do only if actual is inside detectionradius 81 inverseDistance = (0,0,0); 82 inverseDistance = location-actual.location; //calculate the distancevector heading towards this 83 //adaptation of the inverseDistance to the distance 84 if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;} 85 if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;} 86 if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;} 87 if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;} 88 steering = steering + inverseDistance; //add up all significant steeringvectors 89 numberOfNeighbour++; //counts the elements inside the detectionradius 90 } 91 } 92 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> separation steeringvector 93 return steering; 72 94 } 73 95 74 void calculateLocation(const FrameEvent& time) { 75 location = location + speed*time.timeSinceLastFrame; 96 //alignment-function (lead elements to the same heading) 97 Vector3 alignment(Element arrayOfElements[]) { 98 Vector3 steering = Vector3(0,0,0); //steeringvector 99 int numberOfNeighbour = 0; //number of observed neighbours 100 float distance = 0; 101 //go through all elements 102 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 103 Element actual = arrayOfElements[i]; //get the actual element 104 float distance = getDistance(actual); //get distance between this and actual 105 if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) { //check if actual element is inside detectionradius 106 steering = steering + actual.speed; //add up all speedvectors inside the detectionradius 107 numberOfNeighbour++; //counts the elements inside the detectionradius 108 } 109 } 110 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> alignment steeringvector 111 return steering; 76 112 } 77 113 78 79 Vector3 separation(Element arrayOfElements[]) {80 Vector3 * steering = newVector3(0,0,0); //steeringvector114 //cohseion-function (keep elements close to each other) 115 Vector3 cohesion(Element arrayOfElements[]) { 116 Vector3 steering = Vector3(0,0,0); //steeringvector 81 117 int numberOfNeighbour = 0; //number of observed neighbours 118 float distance = 0; 82 119 //go through all elements 83 for(int i=0; i< 3; i++) { //just working with 3 elements at the moment120 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 84 121 Element actual = arrayOfElements[i]; //get the actual element 85 122 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 123 if ((distance > 0) && (distance < COHESIONDISTANCE)) { //check if actual element is inside detectionradius 124 steering = steering + actual.location; //add up all locations of elements inside the detectionradius 92 125 numberOfNeighbour++; //counts the elements inside the detectionradius 93 126 } 94 127 } 95 128 if(numberOfNeighbour > 0) { 96 *steering = *steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> separation steeringvector 129 steering = steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector 130 steering = steering - this->location; //transform the vector for the ship 97 131 } 98 return *steering;132 return steering; 99 133 } 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 134 }; //End of class Element
Note: See TracChangeset
for help on using the changeset viewer.