Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 28, 2007, 4:24:45 PM (16 years ago)
Author:
motth
Message:

added Flocking

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AI/src/Flocking.h

    r233 r325  
     1//
     2//
     3//      TODO: testing orxonox -flocking interface
     4//            testing algorithm
    15
     6// ueberpruefen ob vektoren relativ richtig berechnet werden
     7//
    28//My Flocking Class
    39
     
    713#include <Ogre.h>
    814#include <OgreVector3.h>
     15
     16#include <iostream>
    917
    1018
     
    2230    Vector3 acceleration;  // accelerationvector of the element
    2331
     32  Element() {
     33    acceleration = (0,0,0);
     34    speed = (0,0,0);
     35    location = (0,0,0);
     36  }
    2437
    2538  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_) {
    2645    acceleration = acceleration_;
    2746    speed = speed_;
     
    3655
    3756//EINF[GEN DES ELEMENTS
    38   void update(Element* arrayOfElements) {
     57  void update(Element arrayOfElements[], const FrameEvent& time) {
    3958    calculateAcceleration(arrayOfElements);  //updates the acceleration
    40     calculateSpeed();  //updates the speed
    41     calculateLocation();  //updates the location
     59    calculateSpeed(time);  //updates the speed
     60    calculateLocation(time);  //updates the location
    4261  }
    4362
    4463//EINF[GEN DES ELEMENTS
    45   void calculateAcceleration(Element* arrayOfElements) {
     64  void calculateAcceleration(Element arrayOfElements[]) {
    4665  //calculates the accelerationvector based on the steeringvectors of
    4766  //separtion, alignment and cohesion.
    48   acceleration = acceleration + separation(arrayOfElements) + 2*alignment(arrayOfElements) + 2*cohesion(arrayOfElements);
     67  acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);
    4968  }
    5069
    51   void calculateSpeed() {
    52   speed = speed + acceleration;
    53   //speed = speed.normalise();
     70  void calculateSpeed(const FrameEvent& time) {
     71    speed = speed + acceleration*time.timeSinceLastFrame;
    5472  }
    5573
    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;
    5976  }
    6077
    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
    6482    //go through all elements
    65     for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
     83    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
    6684      Element actual = arrayOfElements[i];  //get the actual element
    6785      float distance = getDistance(actual);  //get distance between this and actual
    68 //DUMMY SEPERATION DETECTION DISTANCE = 25
    69       if ((distance > 0) && (distance<1)) {  //do only if actual is inside detectionradius
     86//DUMMY SEPERATION DETECTION DISTANCE =100
     87      if ((distance > 0) && (distance<100)) {  //do only if actual is inside detectionradius
    7088        Vector3 inverseDistance = actual.location-location;  //calculate the distancevector heading towards this
    7189        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 steeringvectors
     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
    7492        numberOfNeighbour++;  //counts the elements inside the detectionradius
    7593      }
    7694    }
    7795    if(numberOfNeighbour > 0) {
    78     steering = steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
     96    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
    7997    }
    80     return steering;
     98    // cout << *steering << endl;
     99    return *steering;
    81100  }
    82101
    83   Vector3 alignment(Element* arrayOfElements) {
    84     Vector3 steering; //steeringvector
    85     int numberOfNeighbour;  //number of observed neighbours
     102  Vector3 alignment(Element arrayOfElements[]) {
     103    Vector3* steering = new Vector3(0,0,0); //steeringvector
     104    int numberOfNeighbour = 0;  //number of observed neighbours
    86105    //go through all elements
    87     for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
     106    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
    88107      Element actual = arrayOfElements[i];  //get the actual element
    89108      float distance = getDistance(actual);  //get distance between this and actual
    90 //DUMMY ALIGNMENT DETECTION DISTANCE = 50
     109//DUMMY ALIGNMENT DETECTION DISTANCE = 1000
    91110      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
    92         steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
     111        *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
    93112        numberOfNeighbour++;  //counts the elements inside the detectionradius
    94113      }
    95114    }
    96115    if(numberOfNeighbour > 0) {
    97     steering = steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
     116    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
    98117    }
    99     return steering;
     118    cout << *steering << endl;
     119    return *steering;
    100120  }
    101121
    102   Vector3 cohesion(Element* arrayOfElements) {
    103     Vector3 steering; //steeringvector
    104     int numberOfNeighbour;  //number of observed neighbours
     122  Vector3 cohesion(Element arrayOfElements[]) {
     123    Vector3* steering = new Vector3(0,0,0); //steeringvector
     124    int numberOfNeighbour = 0;  //number of observed neighbours
    105125    //go through all elements
    106     for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
     126    for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
    107127      Element actual = arrayOfElements[i];  //get the actual element
    108128      float distance = getDistance(actual);  //get distance between this and actual
    109 // DUMMY COHESION DETECTION DISTANCE = 50
     129// DUMMY COHESION DETECTION DISTANCE = 1000
    110130      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 detectionradius
     131        *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
    112132        numberOfNeighbour++;  //counts the elements inside the detectionradius
    113133      }
    114134     }
    115135    if(numberOfNeighbour > 0) {
    116     steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
     136    *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
    117137    }
     138    return *steering;
    118139  }
    119 
     140 
    120141};
    121142
Note: See TracChangeset for help on using the changeset viewer.