Changeset 627 for code/branches/FICN/src/orxonox
- Timestamp:
- Dec 18, 2007, 10:11:27 PM (17 years ago)
- Location:
- code/branches/FICN/src/orxonox
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/FICN/src/orxonox/Orxonox.cc
r616 r627 63 63 #include "objects/Tickable.h" 64 64 #include "objects/Timer.h" 65 #include "objects/NPC.h" 65 66 #include "core/ArgReader.h" 66 67 #include "core/Factory.h" … … 90 91 { 91 92 auMan_->update(); 93 updateAI(); 92 94 93 95 if(mode_==PRESENTATION) … … 100 102 mKeyboard->capture(); 101 103 return !mKeyboard->isKeyDown(OIS::KC_ESCAPE); 104 } 105 106 void updateAI() 107 { 108 for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) 109 { 110 it->update(); 111 } 102 112 } 103 113 -
code/branches/FICN/src/orxonox/objects/NPC.cc
r619 r627 29 29 #include "../core/Iterator.h" 30 30 #include "../core/ObjectList.h" 31 //#include "../Flocking.h"32 31 33 32 namespace orxonox { 34 33 34 CreateFactory(NPC); 35 35 36 NPC::NPC() 36 37 { 38 RegisterObject(NPC); 37 39 movable_ = true; 38 40 } … … 40 42 NPC::~NPC() 41 43 { 44 } 45 46 void NPC::loadParams(TiXmlElement* xmlElem) 47 { 48 Model::loadParams(xmlElem); 42 49 } 43 50 … … 55 62 * calculates the distance between the element and an other point given by temp 56 63 */ 57 float NPC::getDistance( NPC* temp)64 float NPC::getDistance(WorldEntity* temp) 58 65 { 59 66 Vector3 distance = temp->getPosition() - this->getPosition(); … … 67 74 { 68 75 69 // find out about this arrayOfElements70 NPC* arrayOfElements[ANZELEMENTS];71 72 76 //if element is movable, calculate acceleration 73 if (this->movable_ == true) calculateAcceleration( arrayOfElements);77 if (this->movable_ == true) calculateAcceleration(); 74 78 75 79 } … … 81 85 { 82 86 83 87 this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt); 88 this->translate(this->getVelocity()*dt); 89 this->setAcceleration(Vector3(0,0,0)); 84 90 } 85 91 … … 87 93 * calculates the new acceleration of an element 88 94 */ 89 void NPC::calculateAcceleration( NPC** arrayOfElements)95 void NPC::calculateAcceleration() 90 96 { 91 97 //acceleration consisting of flocking-functions 92 this->setAcceleration(separation( arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements));98 this->setAcceleration(separation() + alignment() + cohesion()); 93 99 } 94 100 … … 96 102 * separation-function (keep elements separated, avoid crashs) 97 103 */ 98 Vector3 NPC::separation( NPC** arrayOfElements)104 Vector3 NPC::separation() 99 105 { 100 106 Vector3 steering = Vector3(0,0,0); //steeringvector … … 102 108 int numberOfNeighbour = 0; //number of observed neighbours 103 109 float distance = 0; // distance to the actual element 104 for(int i=0; i<ANZELEMENTS; i++) { //go through all elements 105 NPC* actual = arrayOfElements[i]; //get the actual element 106 distance = getDistance(actual); //get distance between this and actual 110 for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::start(); it; ++it) { //go through all elements 111 distance = getDistance(*it); //get distance between this and actual 107 112 if ((distance > 0) && (distance < SEPERATIONDISTANCE)) { //do only if actual is inside detectionradius 108 113 inverseDistance = Vector3(0,0,0); 109 inverseDistance = this->getPosition() - actual->getPosition(); //calculate the distancevector heading towards this114 inverseDistance = this->getPosition() - it->getPosition(); //calculate the distancevector heading towards this 110 115 //adaptation of the inverseDistance to the distance 111 116 if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;} … … 124 129 * alignment-function (lead elements to the same heading) 125 130 */ 126 Vector3 NPC::alignment( NPC** arrayOfElements)131 Vector3 NPC::alignment() 127 132 { 128 133 Vector3 steering = Vector3(0,0,0); //steeringvector … … 130 135 //float distance = 0; 131 136 //go through all elements 132 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 133 NPC* actual = arrayOfElements[i]; //get the actual element 134 float distance = getDistance(actual); //get distance between this and actual 137 for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) { //just working with 3 elements at the moment 138 float distance = getDistance(*it); //get distance between this and actual 135 139 if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) { //check if actual element is inside detectionradius 136 steering = steering + actual->getVelocity(); //add up all speedvectors inside the detectionradius140 steering = steering + it->getVelocity(); //add up all speedvectors inside the detectionradius 137 141 numberOfNeighbour++; //counts the elements inside the detectionradius 138 142 } … … 145 149 * cohseion-function (keep elements close to each other) 146 150 */ 147 Vector3 NPC::cohesion( NPC** arrayOfElements)151 Vector3 NPC::cohesion() 148 152 { 149 153 Vector3 steering = Vector3(0,0,0); //steeringvector … … 151 155 //float distance = 0; 152 156 //go through all elements 153 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 154 NPC* actual = arrayOfElements[i]; //get the actual element 155 float distance = getDistance(actual); //get distance between this and actual 157 for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) { //just working with 3 elements at the moment 158 float distance = getDistance(*it); //get distance between this and actual 156 159 if ((distance > 0) && (distance < COHESIONDISTANCE)) { //check if actual element is inside detectionradius 157 steering = steering + actual->getPosition(); //add up all locations of elements inside the detectionradius160 steering = steering + it->getPosition(); //add up all locations of elements inside the detectionradius 158 161 numberOfNeighbour++; //counts the elements inside the detectionradius 159 162 } -
code/branches/FICN/src/orxonox/objects/NPC.h
r619 r627 9 9 10 10 // includes 11 #include "WorldEntity.h"12 11 #include "Model.h" 13 12 … … 20 19 NPC(); 21 20 virtual ~NPC(); 21 virtual void loadParams(TiXmlElement* xmlElem); 22 22 void tick(float dt); 23 23 void update(); … … 25 25 26 26 private: 27 float getDistance( NPC* temp);28 void calculateAcceleration( NPC** arrayOfElements);29 Vector3 separation( NPC** arrayOfElements);30 Vector3 alignment( NPC** arrayOfElements);31 Vector3 cohesion( NPC** arrayOfElements);27 float getDistance(WorldEntity* temp); 28 void calculateAcceleration(); 29 Vector3 separation(); 30 Vector3 alignment(); 31 Vector3 cohesion(); 32 32 33 33
Note: See TracChangeset
for help on using the changeset viewer.