Changeset 495
- Timestamp:
- Dec 12, 2007, 10:37:06 PM (17 years ago)
- Location:
- code/branches/AI/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI/src/AIClass.h
r426 r495 1 #ifndef Flocking_Class2 #define Flocking_Class1 #ifndef AI_Class 2 #define AI_Class 3 3 4 4 #include <Ogre.h> -
code/branches/AI/src/Arrival.h
r426 r495 3 3 4 4 5 #ifndef Flocking_Class6 #define Flocking_Class5 #ifndef Arrival_Class 6 #define Arrical_Class 7 7 8 8 #include <Ogre.h> … … 59 59 60 60 double relativeDirectApproach() { 61 // Maxspeed / accelerationForwards = time needed to break with max acceleration 62 // 2*getDistance()length/(MaxSpeed/accelerationForwards)^2 = required acceleration to arrive at the target with speed = 0 61 63 return (accelerationForwards / (2*getDirection().length / (MaxSpeed/accelerationForwards)^2) ); 62 64 } … … 65 67 Quaternion rotation = (0,0,0,0); 66 68 if (relativeDirectApproach() > 1) { 67 rotation = speed.getRotationTo(getDirection()); 68 // do that turn 69 69 float length = speed.length(); 70 speed = (speed+getDirection()); 71 speed.normalise(); 72 speed = speed*length; 73 if (relativeDirectApproach > 4) { 74 //accelerate 75 } 76 else { 77 // speed will stay constant 78 } 79 80 70 81 } 71 82 else { 83 72 84 73 85 } -
code/branches/AI/src/Flocking.h
r426 r495 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 … … 20 14 #endif 21 15 22 using namespace std;23 16 using namespace Ogre; 24 17 … … 30 23 Vector3 speed; // speedvector of the element 31 24 Vector3 acceleration; // accelerationvector of the element 32 bool movable; // movability 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 33 30 31 //default constructor 34 32 Element() { 35 33 acceleration = (0,0,0); … … 39 37 } 40 38 39 //constructor 41 40 Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) { 42 41 acceleration = acceleration_; … … 46 45 } 47 46 47 //function to chance values of an element 48 48 void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) { 49 49 acceleration = acceleration_; … … 55 55 //calculates the distance between the element and an other point given by temp 56 56 float getDistance(Element temp) { 57 Vector3 distance = temp.location-location; //this doesn't work57 Vector3 distance = temp.location-location; 58 58 return distance.length(); 59 59 } 60 60 61 //EINFÜGEN DES ELEMENTS 62 void update(Element arrayOfElements[], const FrameEvent& time) { 63 if (this->movable == true) {calculateAcceleration(arrayOfElements);} 64 65 /* if (this->movable == true) { 66 calculateAcceleration(arrayOfElements); //updates the acceleration 67 calculateSpeed(time); //updates the speed 68 calculateLocation(time); //updates the location 69 } */ 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 70 64 } 71 65 72 //EINFÜGEN DES ELEMENTS 66 //calculates the new acceleration of an element 73 67 void calculateAcceleration(Element arrayOfElements[]) { 74 //calculates the accelerationvector based on the steeringvectors of 75 //separtion, alignment and cohesion. 76 acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements); 68 acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements); //acceleration consisting of flocking-functions 77 69 } 78 70 79 void calculateSpeed(const FrameEvent& time) { 80 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; 81 94 } 82 95 83 void calculateLocation(const FrameEvent& time) { 84 location = location + speed*time.timeSinceLastFrame; 85 } 86 87 88 Vector3 separation(Element arrayOfElements[]) { 89 Vector3* steering = new Vector3(0,0,0); //steeringvector 90 Vector3* inverseDistance = new Vector3(0,0,0); 96 //alignment-function (lead elements to the same heading) 97 Vector3 alignment(Element arrayOfElements[]) { 98 Vector3 steering = Vector3(0,0,0); //steeringvector 91 99 int numberOfNeighbour = 0; //number of observed neighbours 92 100 float distance = 0; 93 101 //go through all elements 94 for(int i=0; i< 9; i++) { //just working with 3 elements at the moment102 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 95 103 Element actual = arrayOfElements[i]; //get the actual element 96 distance = getDistance(actual); //get distance between this and actual 97 //DUMMY SEPERATION DETECTION DISTANCE =100 98 if ((distance > 0) && (distance < 200)) { //do only if actual is inside detectionradius 99 *inverseDistance = (0,0,0); 100 *inverseDistance = location-actual.location; //calculate the distancevector heading towards this 101 //*inverseDistance = inverseDistance->normalise(); //does this work correctly? //normalise the distancevector 102 if ((distance < 100) && (distance >= 80)) {*inverseDistance = *inverseDistance*2;} 103 if ((distance < 80) && (distance >= 60)) {*inverseDistance = *inverseDistance*5;} 104 if ((distance < 60) && (distance >= 40)) {*inverseDistance = *inverseDistance*10;} 105 if ((distance < 40) && (distance > 0)) {*inverseDistance = *inverseDistance*20;} 106 // *inverseDistance = *inverseDistance/distance; //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector) 107 *steering = *steering + *inverseDistance; //add up all significant steeringvectors 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; 112 } 113 114 //cohseion-function (keep elements close to each other) 115 Vector3 cohesion(Element arrayOfElements[]) { 116 Vector3 steering = Vector3(0,0,0); //steeringvector 117 int numberOfNeighbour = 0; //number of observed neighbours 118 float distance = 0; 119 //go through all elements 120 for(int i=0; i<ANZELEMENTS; i++) { //just working with 3 elements at the moment 121 Element actual = arrayOfElements[i]; //get the actual element 122 float distance = getDistance(actual); //get distance between this and actual 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 108 125 numberOfNeighbour++; //counts the elements inside the detectionradius 109 126 } 110 127 } 111 128 if(numberOfNeighbour > 0) { 112 *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 113 131 } 114 cout<<*steering<<endl; 115 return *steering; 132 return steering; 116 133 } 117 118 Vector3 alignment(Element arrayOfElements[]) { 119 Vector3* steering = new 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<9; i++) { //just working with 3 elements at the moment 124 Element actual = arrayOfElements[i]; //get the actual element 125 float distance = getDistance(actual); //get distance between this and actual 126 //DUMMY ALIGNMENT DETECTION DISTANCE = 1000 127 if ((distance > 0) && (distance < 300)) { //check if actual element is inside detectionradius 128 *steering = *steering + actual.speed; //add up all speedvectors inside the detectionradius 129 numberOfNeighbour++; //counts the elements inside the detectionradius 130 } 131 } 132 if(numberOfNeighbour > 0) { 133 *steering = *steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> alignment steeringvector 134 } 135 return *steering; 136 } 137 138 Vector3 cohesion(Element arrayOfElements[]) { 139 Vector3* steering = new Vector3(0,0,0); //steeringvector 140 int numberOfNeighbour = 0; //number of observed neighbours 141 float distance = 0; 142 //go through all elements 143 for(int i=0; i<9; i++) { //just working with 3 elements at the moment 144 Element actual = arrayOfElements[i]; //get the actual element 145 float distance = getDistance(actual); //get distance between this and actual 146 // DUMMY COHESION DETECTION DISTANCE = 1000 147 if ((distance > 0) && (distance < 5000)) { //check if actual element is inside detectionradius 148 *steering = *steering + actual.location; //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->location; // (?) Koordinatensystem? 155 } 156 return *steering; 157 } 158 }; 159 160 161 162 //End of My Flocking Class 134 }; //End of class Element -
code/branches/AI/src/orxonox.cc
r468 r495 44 44 #include "loader/LevelLoader.h" 45 45 #include "Flocking.h" 46 #include " AIClass.h"46 #include "Wander.h" 47 47 48 48 // some tests to see if enet works without includsion … … 86 86 87 87 Element arrayOfElements[9]; 88 89 // float time = 0; 88 Wander walker; 89 int counter = 0; 90 int times = 0; 91 90 92 91 93 … … 110 112 SceneManager *mgr = root_->getSceneManager("Default SceneManager"); 111 113 112 arrayOfElements[0].update(arrayOfElements, evt); 113 arrayOfElements[1].update(arrayOfElements, evt); 114 arrayOfElements[2].update(arrayOfElements, evt); 115 arrayOfElements[3].update(arrayOfElements, evt); 116 arrayOfElements[4].update(arrayOfElements, evt); 117 arrayOfElements[5].update(arrayOfElements, evt); 118 arrayOfElements[6].update(arrayOfElements, evt); 119 arrayOfElements[7].update(arrayOfElements, evt); 120 arrayOfElements[8].update(arrayOfElements, evt); 121 122 /* arrayOfElements[0].update(arrayOfElements, evt); 123 arrayOfElements[1].update(arrayOfElements, evt); 124 arrayOfElements[2].update(arrayOfElements, evt); 125 arrayOfElements[3].update(arrayOfElements, evt); 126 arrayOfElements[4].update(arrayOfElements, evt); */ 127 114 /* // RUN WANDER 115 116 walker.update(); 117 walker.speed = walker.speed + 10*walker.acceleration*evt.timeSinceLastFrame; 118 walker.location = walker.location + 10*walker.speed*evt.timeSinceLastFrame; 119 walker.acceleration = (0,0,0); 120 mgr->getSceneNode("HeadNode10")->setPosition(walker.location); 121 122 */ // END RUN WANDER 123 124 125 126 127 // RUN FLOCKING 128 129 arrayOfElements[8].location = 100*Vector3(Math::Cos(Math::DegreesToRadians(counter)/10),Math::Sin(Math::DegreesToRadians(counter)/10),Math::Cos(Math::DegreesToRadians(counter+(counter-180)/2)/10)); 130 131 arrayOfElements[0].update(arrayOfElements); 132 arrayOfElements[1].update(arrayOfElements); 133 arrayOfElements[2].update(arrayOfElements); 134 arrayOfElements[3].update(arrayOfElements); 135 arrayOfElements[4].update(arrayOfElements); 136 arrayOfElements[5].update(arrayOfElements); 137 arrayOfElements[6].update(arrayOfElements); 138 arrayOfElements[7].update(arrayOfElements); 139 arrayOfElements[8].update(arrayOfElements); 128 140 129 141 for(int i=0; i<9; i++) { 130 131 arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame; 132 133 arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame; 134 135 arrayOfElements[i].acceleration = (0,0,0); 142 arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame; 143 arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame; 144 arrayOfElements[i].acceleration = (0,0,0); 136 145 } 137 146 … … 146 155 mgr->getSceneNode("HeadNode9")->setPosition(arrayOfElements[8].location); 147 156 148 149 /* 150 151 mgr->getSceneNode("HeadNode9")->setPosition(Vector3(200*cos(10*time),0,0)); 152 time = time + evt.timeSinceLastFrame; 153 154 */ 155 156 157 158 // mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame); 157 counter = counter + 1; 158 counter = counter%7200; 159 160 // END RUN FLOCKING 161 159 162 } 160 163 … … 272 275 cam->lookAt(Vector3(0,0,0)); 273 276 Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam); 274 example(); //my stuff 277 278 //Invoke example to test AI 279 example(); 275 280 } 276 281 … … 321 326 } 322 327 323 //declaration of the 3 Ogreheads324 //muss leider global sein.....325 //Element* arrayOfElements[2];326 327 328 void example() { 328 329 SceneManager *mgr = mRoot->getSceneManager("Default SceneManager"); 329 330 mgr->setAmbientLight(ColourValue(1.0,1.0,1.0)); 331 332 /* //TEST DATA WANDER 333 334 Entity* ent10 = mgr->createEntity("Head10", "ogrehead.mesh"); 335 SceneNode *node10 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode10", Vector3(0,0,0)); 336 node10->attachObject(ent10); 337 Vector3 temp; 338 temp = (0,0,0); 339 walker.setValues(node10->getPosition(),temp,temp,true); 340 341 */ //END TEST DATA WANDER 342 343 344 // TEST DATA FLOCKING 330 345 331 346 Entity* ent1 = mgr->createEntity("Head1", "ogrehead.mesh"); … … 347 362 SceneNode *node7 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode7", Vector3(-150,-150,0)); 348 363 SceneNode *node8 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode8", Vector3(-150,150,0)); 349 SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0)); 350 351 // follwing camera 352 353 // Camera *cam = mgr->getCamera("Camera"); 354 // node1->attachObject(cam); 355 356 357 364 SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0)); 358 365 359 366 node1->attachObject(ent1); … … 376 383 ElementLocationArray[7] = node8->getPosition(); 377 384 ElementLocationArray[8] = node9->getPosition(); 378 /* 379 ElementLocationArray[5] = node6->getPosition(); 380 ElementLocationArray[6] = node7->getPosition();*/ 385 381 386 ElementSpeedArray[0] = (0,0,0); 382 387 ElementSpeedArray[1] = (0,0,0); … … 388 393 ElementSpeedArray[7] = (0,0,0); 389 394 ElementSpeedArray[8] = (0,0,0); 390 /* 391 ElementSpeedArray[5] = (0,0,0); 392 ElementSpeedArray[6] = (0,0,0); */ 395 393 396 ElementAccelerationArray[0] = (0,0,0); 394 397 ElementAccelerationArray[1] = (0,0,0); … … 400 403 ElementAccelerationArray[7] = (0,0,0); 401 404 ElementAccelerationArray[8] = (0,0,0); 402 /* 403 ElementAccelerationArray[5] = (0,0,0); 404 ElementAccelerationArray[6] = (0,0,0); */ 405 405 406 arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0], true); 406 407 arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1], true); … … 412 413 arrayOfElements[7].setValues( ElementLocationArray[7], ElementSpeedArray[7], ElementAccelerationArray[7], true); 413 414 arrayOfElements[8].setValues( ElementLocationArray[8], ElementSpeedArray[8], ElementAccelerationArray[8], false); 414 /* 415 arrayOfElements[5].setValues( ElementLocationArray[5], ElementSpeedArray[5], ElementAccelerationArray[5], false); 416 arrayOfElements[6].setValues( ElementLocationArray[6], ElementSpeedArray[6], ElementAccelerationArray[6], false);*/ 417 418 419 420 421 /* for (int i=0; i<3; i++) { 422 Element* arrayOfElements[i] = new Element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] ); 423 } */ 424 /* for (int i=0; i<3; i++) { 425 arrayOfElements[i]->update(arrayOfElements); 426 } */ 427 428 //testing AIPilot -> function steer 429 // AIPilot temp; 430 // Vector3 foo = temp.steer(Vector3(0,0,1)); 415 416 // END TEST DATA FLOCKING 431 417 432 418
Note: See TracChangeset
for help on using the changeset viewer.