Changeset 617
- Timestamp:
- Dec 18, 2007, 4:41:20 PM (17 years ago)
- Location:
- code/branches/FICN/src/orxonox/objects
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/orxonox/objects/NPC.cc
r603 r617 29 29 #include "../core/Iterator.h" 30 30 #include "../core/ObjectList.h" 31 #include "../Flocking.h"31 //#include "../Flocking.h" 32 32 33 33 namespace orxonox { … … 35 35 NPC::NPC() 36 36 { 37 movable_ = true; 37 38 } 38 39 … … 41 42 } 42 43 43 } 44 /** 45 * function to chance values of an element 46 */ 47 void NPC::setValues(Vector3 location, Vector3 speed, Vector3 acceleration, bool movable) { 48 this->setAcceleration(acceleration); 49 this->setVelocity(speed); 50 this->translate(location); 51 movable_ = movable; 52 } 53 54 /** 55 * calculates the distance between the element and an other point given by temp 56 */ 57 float NPC::getDistance(NPC* temp) 58 { 59 Vector3 distance = temp->getPosition() - this->getPosition(); 60 return distance.length(); 61 } 62 63 /** 64 * updates the data of an element 65 */ 66 void NPC::tick(float dt) 67 { 68 69 // find out about this arrayOfElements 70 NPC* arrayOfElements[ANZELEMENTS]; 71 72 //if element is movable, calculate acceleration 73 if (this->movable_ == true) calculateAcceleration(arrayOfElements); 74 75 } 76 77 /** 78 * calculates the new acceleration of an element 79 */ 80 void NPC::calculateAcceleration(NPC** arrayOfElements) 81 { 82 //acceleration consisting of flocking-functions 83 this->setAcceleration(separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements)); 84 } 85 86 /** 87 * separation-function (keep elements separated, avoid crashs) 88 */ 89 Vector3 NPC::separation(NPC** arrayOfElements) 90 { 91 Vector3 steering = Vector3(0,0,0); //steeringvector 92 Vector3 inverseDistance = Vector3(0,0,0); //vector pointing away from possible collisions 93 int numberOfNeighbour = 0; //number of observed neighbours 94 float distance = 0; // distance to the actual element 95 for(int i=0; i<ANZELEMENTS; i++) { //go through all elements 96 NPC* actual = arrayOfElements[i]; //get the actual element 97 distance = getDistance(actual); //get distance between this and actual 98 if ((distance > 0) && (distance < SEPERATIONDISTANCE)) { //do only if actual is inside detectionradius 99 inverseDistance = Vector3(0,0,0); 100 inverseDistance = this->getPosition() - actual->getPosition(); //calculate the distancevector heading towards this 101 //adaptation of the inverseDistance to the distance 102 if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;} 103 if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;} 104 if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;} 105 if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;} 106 steering = steering + inverseDistance; //add up all significant steeringvectors 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 -> separation steeringvector 111 return steering; 112 } 113 114 /** 115 * alignment-function (lead elements to the same heading) 116 */ 117 Vector3 NPC::alignment(NPC** arrayOfElements) 118 { 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 NPC* actual = arrayOfElements[i]; //get the actual element 125 float distance = getDistance(actual); //get distance between this and actual 126 if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) { //check if actual element is inside detectionradius 127 steering = steering + actual->getVelocity(); //add up all speedvectors inside the detectionradius 128 numberOfNeighbour++; //counts the elements inside the detectionradius 129 } 130 } 131 if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; } //devide the sum of steeringvectors by the number of elements -> alignment steeringvector 132 return steering; 133 } 134 135 /** 136 * cohseion-function (keep elements close to each other) 137 */ 138 Vector3 NPC::cohesion(NPC** arrayOfElements) 139 { 140 Vector3 steering = Vector3(0,0,0); //steeringvector 141 int numberOfNeighbour = 0; //number of observed neighbours 142 //float distance = 0; 143 //go through all elements 144 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 145 NPC* actual = arrayOfElements[i]; //get the actual element 146 float distance = getDistance(actual); //get distance between this and actual 147 if ((distance > 0) && (distance < COHESIONDISTANCE)) { //check if actual element is inside detectionradius 148 steering = steering + actual->getPosition(); //add up all locations of elements inside the detectionradius 149 numberOfNeighbour++; //counts the elements inside the detectionradius 150 } 151 } 152 if(numberOfNeighbour > 0) { 153 steering = steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector 154 steering = steering - this->getPosition(); //transform the vector for the ship 155 } 156 return steering; 157 } 158 159 } // end of class NPC -
code/branches/FICN/src/orxonox/objects/NPC.h
r603 r617 10 10 // includes 11 11 #include "WorldEntity.h" 12 #include "Model.h" 12 13 13 14 namespace orxonox { 14 15 15 class NPC : public WorldEntity16 class NPC : public Model 16 17 { 17 NPC(); 18 virtual ~NPC(); 18 public: 19 20 NPC(); 21 virtual ~NPC(); 22 void tick(float dt); 23 void setValues(Vector3 location, Vector3 speed, Vector3 acceleration, bool movable); 24 25 private: 26 float getDistance(NPC* temp); 27 void calculateAcceleration(NPC** arrayOfElements); 28 Vector3 separation(NPC** arrayOfElements); 29 Vector3 alignment(NPC** arrayOfElements); 30 Vector3 cohesion(NPC** arrayOfElements); 31 32 33 private: 34 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 35 36 // those values should maybe be controlled by a higher power and should maybe be parameters that can be changed 37 static int const SEPERATIONDISTANCE = 300; //detectionradius of seperation 38 static int const ALIGNMENTDISTANCE = 300; //detectionradius of alignment 39 static int const COHESIONDISTANCE = 5000; //detectionradius of cohesion 40 static int const ANZELEMENTS = 9; //number of elements 19 41 }; 20 42
Note: See TracChangeset
for help on using the changeset viewer.