Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/Flocking.h @ 471

Last change on this file since 471 was 336, checked in by bknecht, 17 years ago

added flocking.h for AI

File size: 5.2 KB
RevLine 
[325]1//
2//
3//      TODO: testing orxonox -flocking interface
4//            testing algorithm
[212]5
[325]6// ueberpruefen ob vektoren relativ richtig berechnet werden
7//
[212]8//My Flocking Class
9
10#ifndef Flocking_Class
11#define Flocking_Class
12
13#include <Ogre.h>
14#include <OgreVector3.h>
15
[325]16#include <iostream>
[212]17
[325]18
[212]19#endif
20
21using namespace std;
22using namespace Ogre;
23
24class Element // An element that flocks
25{
26
27  public:
28    Vector3 location;  // locationvector of the element
29    Vector3 speed;  // speedvector of the element
30    Vector3 acceleration;  // accelerationvector of the element
31
[325]32  Element() {
33    acceleration = (0,0,0);
34    speed = (0,0,0);
35    location = (0,0,0);
36  }
[212]37
[233]38  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
[212]39    acceleration = acceleration_;
40    speed = speed_;
41    location = location_;
42  }
43
[325]44  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
45    acceleration = acceleration_;
46    speed = speed_;
47    location = location_;
48  }
49
[212]50  //calculates the distance between the element and an other point given by temp
51  float getDistance(Element temp) {
52    Vector3 distance = temp.location-location;  //this doesn't work
53    return distance.length();
54  }
55
56//EINF[GEN DES ELEMENTS
[325]57  void update(Element arrayOfElements[], const FrameEvent& time) {
[233]58    calculateAcceleration(arrayOfElements);  //updates the acceleration
[325]59    calculateSpeed(time);  //updates the speed
60    calculateLocation(time);  //updates the location
[212]61  }
62
63//EINF[GEN DES ELEMENTS
[325]64  void calculateAcceleration(Element arrayOfElements[]) {
[212]65  //calculates the accelerationvector based on the steeringvectors of
66  //separtion, alignment and cohesion.
[325]67  acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);
[212]68  }
69
[325]70  void calculateSpeed(const FrameEvent& time) {
71    speed = speed + acceleration*time.timeSinceLastFrame;
[212]72  }
73
[325]74  void calculateLocation(const FrameEvent& time) {
75    location = location + speed*time.timeSinceLastFrame;
[212]76  }
77
[325]78
79  Vector3 separation(Element arrayOfElements[]) {
80    Vector3* steering = new Vector3(0,0,0); //steeringvector
81    int numberOfNeighbour = 0;  //number of observed neighbours
[212]82    //go through all elements
[325]83    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
[212]84      Element actual = arrayOfElements[i];  //get the actual element
[233]85      float distance = getDistance(actual);  //get distance between this and actual
[325]86//DUMMY SEPERATION DETECTION DISTANCE =100
87      if ((distance > 0) && (distance<100)) {  //do only if actual is inside detectionradius
[212]88        Vector3 inverseDistance = actual.location-location;  //calculate the distancevector heading towards this
89        inverseDistance = inverseDistance.normalise(); //does this work correctly?  //normalise the distancevector
[325]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
[212]92        numberOfNeighbour++;  //counts the elements inside the detectionradius
93      }
94    }
95    if(numberOfNeighbour > 0) {
[325]96    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
[212]97    }
[325]98    return *steering;
[212]99  }
100
[325]101  Vector3 alignment(Element arrayOfElements[]) {
102    Vector3* steering = new Vector3(0,0,0); //steeringvector
103    int numberOfNeighbour = 0;  //number of observed neighbours
[212]104    //go through all elements
[325]105    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
[212]106      Element actual = arrayOfElements[i];  //get the actual element
[233]107      float distance = getDistance(actual);  //get distance between this and actual
[325]108//DUMMY ALIGNMENT DETECTION DISTANCE = 1000
[233]109      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
[325]110        *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
[212]111        numberOfNeighbour++;  //counts the elements inside the detectionradius
112      }
113    }
114    if(numberOfNeighbour > 0) {
[325]115    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
[212]116    }
[325]117    return *steering;
[212]118  }
119
[325]120  Vector3 cohesion(Element arrayOfElements[]) {
121    Vector3* steering = new Vector3(0,0,0); //steeringvector
122    int numberOfNeighbour = 0;  //number of observed neighbours
[212]123    //go through all elements
[325]124    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
[212]125      Element actual = arrayOfElements[i];  //get the actual element
[233]126      float distance = getDistance(actual);  //get distance between this and actual
[325]127// DUMMY COHESION DETECTION DISTANCE = 1000
[233]128      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
[325]129        *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
[212]130        numberOfNeighbour++;  //counts the elements inside the detectionradius
131      }
132     }
133    if(numberOfNeighbour > 0) {
[325]134    *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
[212]135    }
[325]136    return *steering;
[212]137  }
[325]138 
[212]139};
140
141
142
143//End of My Flocking Class
Note: See TracBrowser for help on using the repository browser.