Changeset 325 for code/branches/AI
- Timestamp:
- Nov 28, 2007, 4:24:45 PM (17 years ago)
- Location:
- code/branches/AI/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/AI/src/Flocking.h
r233 r325 1 // 2 // 3 // TODO: testing orxonox -flocking interface 4 // testing algorithm 1 5 6 // ueberpruefen ob vektoren relativ richtig berechnet werden 7 // 2 8 //My Flocking Class 3 9 … … 7 13 #include <Ogre.h> 8 14 #include <OgreVector3.h> 15 16 #include <iostream> 9 17 10 18 … … 22 30 Vector3 acceleration; // accelerationvector of the element 23 31 32 Element() { 33 acceleration = (0,0,0); 34 speed = (0,0,0); 35 location = (0,0,0); 36 } 24 37 25 38 Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) { 39 acceleration = acceleration_; 40 speed = speed_; 41 location = location_; 42 } 43 44 void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_) { 26 45 acceleration = acceleration_; 27 46 speed = speed_; … … 36 55 37 56 //EINF[GEN DES ELEMENTS 38 void update(Element * arrayOfElements) {57 void update(Element arrayOfElements[], const FrameEvent& time) { 39 58 calculateAcceleration(arrayOfElements); //updates the acceleration 40 calculateSpeed( ); //updates the speed41 calculateLocation( ); //updates the location59 calculateSpeed(time); //updates the speed 60 calculateLocation(time); //updates the location 42 61 } 43 62 44 63 //EINF[GEN DES ELEMENTS 45 void calculateAcceleration(Element * arrayOfElements) {64 void calculateAcceleration(Element arrayOfElements[]) { 46 65 //calculates the accelerationvector based on the steeringvectors of 47 66 //separtion, alignment and cohesion. 48 acceleration = acceleration + separation(arrayOfElements) + 2*alignment(arrayOfElements) + 2*cohesion(arrayOfElements);67 acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements); 49 68 } 50 69 51 void calculateSpeed() { 52 speed = speed + acceleration; 53 //speed = speed.normalise(); 70 void calculateSpeed(const FrameEvent& time) { 71 speed = speed + acceleration*time.timeSinceLastFrame; 54 72 } 55 73 56 void calculateLocation() { 57 location = location + speed; 58 acceleration = (0,0,0); //set acceleration to zero for the next calculation 74 void calculateLocation(const FrameEvent& time) { 75 location = location + speed*time.timeSinceLastFrame; 59 76 } 60 77 61 Vector3 separation(Element* arrayOfElements) { 62 Vector3 steering; //steeringvector 63 int numberOfNeighbour; //number of observed neighbours 78 79 Vector3 separation(Element arrayOfElements[]) { 80 Vector3* steering = new Vector3(0,0,0); //steeringvector 81 int numberOfNeighbour = 0; //number of observed neighbours 64 82 //go through all elements 65 for(int i= 1; i<3; i++) { //just working with 3 elements at the moment83 for(int i=0; i<3; i++) { //just working with 3 elements at the moment 66 84 Element actual = arrayOfElements[i]; //get the actual element 67 85 float distance = getDistance(actual); //get distance between this and actual 68 //DUMMY SEPERATION DETECTION DISTANCE = 2569 if ((distance > 0) && (distance<1 )) { //do only if actual is inside detectionradius86 //DUMMY SEPERATION DETECTION DISTANCE =100 87 if ((distance > 0) && (distance<100)) { //do only if actual is inside detectionradius 70 88 Vector3 inverseDistance = actual.location-location; //calculate the distancevector heading towards this 71 89 inverseDistance = inverseDistance.normalise(); //does this work correctly? //normalise the distancevector 72 inverseDistance = inverseDistance/*/distance ;*/; //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)73 steering =steering + inverseDistance; //add up all significant steeringvectors90 inverseDistance = inverseDistance/*/distance*/; //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector) 91 *steering = *steering + inverseDistance; //add up all significant steeringvectors 74 92 numberOfNeighbour++; //counts the elements inside the detectionradius 75 93 } 76 94 } 77 95 if(numberOfNeighbour > 0) { 78 steering =steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> separation steeringvector96 *steering = *steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> separation steeringvector 79 97 } 80 return steering; 98 // cout << *steering << endl; 99 return *steering; 81 100 } 82 101 83 Vector3 alignment(Element * arrayOfElements) {84 Vector3 steering; //steeringvector85 int numberOfNeighbour ; //number of observed neighbours102 Vector3 alignment(Element arrayOfElements[]) { 103 Vector3* steering = new Vector3(0,0,0); //steeringvector 104 int numberOfNeighbour = 0; //number of observed neighbours 86 105 //go through all elements 87 for(int i= 1; i<3; i++) { //just working with 3 elements at the moment106 for(int i=0; i<3; i++) { //just working with 3 elements at the moment 88 107 Element actual = arrayOfElements[i]; //get the actual element 89 108 float distance = getDistance(actual); //get distance between this and actual 90 //DUMMY ALIGNMENT DETECTION DISTANCE = 50109 //DUMMY ALIGNMENT DETECTION DISTANCE = 1000 91 110 if ((distance > 0) && (distance<1000)) { //check if actual element is inside detectionradius 92 steering =steering + actual.speed; //add up all speedvectors inside the detectionradius111 *steering = *steering + actual.speed; //add up all speedvectors inside the detectionradius 93 112 numberOfNeighbour++; //counts the elements inside the detectionradius 94 113 } 95 114 } 96 115 if(numberOfNeighbour > 0) { 97 steering =steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> alignment steeringvector116 *steering = *steering / (float)numberOfNeighbour; //devide the sum of steeringvectors by the number of elements -> alignment steeringvector 98 117 } 99 return steering; 118 cout << *steering << endl; 119 return *steering; 100 120 } 101 121 102 Vector3 cohesion(Element * arrayOfElements) {103 Vector3 steering; //steeringvector104 int numberOfNeighbour ; //number of observed neighbours122 Vector3 cohesion(Element arrayOfElements[]) { 123 Vector3* steering = new Vector3(0,0,0); //steeringvector 124 int numberOfNeighbour = 0; //number of observed neighbours 105 125 //go through all elements 106 for(int i= 1; i<3; i++) { //just working with 3 elements at the moment126 for(int i=0; i<3; i++) { //just working with 3 elements at the moment 107 127 Element actual = arrayOfElements[i]; //get the actual element 108 128 float distance = getDistance(actual); //get distance between this and actual 109 // DUMMY COHESION DETECTION DISTANCE = 50129 // DUMMY COHESION DETECTION DISTANCE = 1000 110 130 if ((distance > 0) && (distance<1000)) { //check if actual element is inside detectionradius 111 steering =steering + actual.location; //add up all locations of elements inside the detectionradius131 *steering = *steering + actual.location; //add up all locations of elements inside the detectionradius 112 132 numberOfNeighbour++; //counts the elements inside the detectionradius 113 133 } 114 134 } 115 135 if(numberOfNeighbour > 0) { 116 steering =steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector136 *steering = *steering / (float)numberOfNeighbour; //devide the sum steeringvector by the number of elements -> cohesion steeringvector 117 137 } 138 return *steering; 118 139 } 119 140 120 141 }; 121 142 -
code/branches/AI/src/orxonox.cc
r233 r325 78 78 //my-stuff 79 79 //globale definition eines Arrays welches alle nodes enthält 80 Vector3 ElementLocationArray[ 2];81 Vector3 ElementSpeedArray[ 2];82 Vector3 ElementAccelerationArray[ 2];83 84 Element * arrayOfElements[2];80 Vector3 ElementLocationArray[3]; 81 Vector3 ElementSpeedArray[3]; 82 Vector3 ElementAccelerationArray[3]; 83 84 Element arrayOfElements[3]; 85 85 86 86 … … 104 104 void moving(const FrameEvent& evt) { 105 105 SceneManager *mgr = root_->getSceneManager("Default SceneManager"); 106 arrayOfElements[0]->update(*arrayOfElements); 107 mgr->getSceneNode("HeadNode1")->translate(0.000000001*evt.timeSinceLastFrame*arrayOfElements[0]->location); 108 arrayOfElements[1]->update(*arrayOfElements); 109 mgr->getSceneNode("HeadNode2")->translate(0.000000001*evt.timeSinceLastFrame*arrayOfElements[1]->location); 110 arrayOfElements[2]->update(*arrayOfElements); 111 mgr->getSceneNode("HeadNode3")->translate(0.000000001*evt.timeSinceLastFrame*arrayOfElements[2]->location); 112 //mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame); 106 107 108 109 arrayOfElements[0].update(arrayOfElements, evt); 110 arrayOfElements[1].update(arrayOfElements, evt); 111 arrayOfElements[2].update(arrayOfElements, evt); 112 113 mgr->getSceneNode("HeadNode1")->setPosition(arrayOfElements[0].location); 114 mgr->getSceneNode("HeadNode2")->setPosition(arrayOfElements[1].location); 115 mgr->getSceneNode("HeadNode3")->setPosition(arrayOfElements[2].location); 116 117 118 119 // mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame); 113 120 } 114 121 … … 291 298 node2->attachObject(ent2); 292 299 node3->attachObject(ent3); 293 //Camera* cam = mgr->getCamera("Camera");294 //node1->attachObject(cam);295 300 ElementLocationArray[0] = node1->getPosition(); 296 301 ElementLocationArray[1] = node2->getPosition(); … … 302 307 ElementAccelerationArray[1] = (0,0,0); 303 308 ElementAccelerationArray[2] = (0,0,0); 304 arrayOfElements[0] = new Element( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0] );305 arrayOfElements[1] = new Element( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1] );306 arrayOfElements[2] = new Element( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2] );309 arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0] ); 310 arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1] ); 311 arrayOfElements[2].setValues( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2] ); 307 312 308 313
Note: See TracChangeset
for help on using the changeset viewer.