Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 28, 2007, 10:30:29 PM (16 years ago)
Author:
rgrieder
Message:
  • added Vector2, Vector3, Matrix3, ColourValue, Quaternion and String to the misc folder as header files (each of them contains #include <string> … typedef std::string String , etc.)
  • please use String from now on by including <misc/String.h"
  • removed #include <OgreVector3.h", etc. from "CoreIncludes.h" (adjusted all source files)
  • adjusted all the source files (except network, that keeps <string> for the moment) (what a mess..)
  • moved usleep hack to misc/Sleep.h
  • relative include paths for files from other root directories (like misc, network, etc.) (but it stills writes "../Orxonox.h" when in folder orxonox/objects)
  • "OgreSceneManager.h" —> <OgreSceneManager.h>
  • included OrxonoxPrereqs in every file in folder orxonox
  • moved HUD and ParticleInterface to namespace orxonox
  • removed some using namespace Ogre/std when appropriate
  • I hope I haven't forgotten important points..
File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/orxonox/Flocking.h

    r673 r708  
    55#define _Flocking_H__
    66
    7 // #include <Ogre.h>
    8 #include <OgreVector3.h>
     7#include "misc/Vector3.h"
    98
    10 
    11 #include <iostream>
    12 
    13 
    14 
    15 class Element // An element that flocks
     9namespace orxonox
    1610{
     11  class Element // An element that flocks
     12  {
    1713
    1814  public:
    19     Ogre::Vector3 location;                       //!< locationvector of the element
    20     Ogre::Vector3 speed;                          //!< speedvector of the element
    21     Ogre::Vector3 acceleration;                   //!< accelerationvector of the element
     15    Vector3 location;                       //!< locationvector of the element
     16    Vector3 speed;                          //!< speedvector of the element
     17    Vector3 acceleration;                   //!< accelerationvector of the element
    2218    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
    2319    static int const SEPERATIONDISTANCE = 300;    //!< detectionradius of seperation
     
    2622    static int const ANZELEMENTS = 9;             //!< number of elements
    2723
    28   //! default constructor
    29   Element() {
    30     acceleration = Ogre::Vector3(0,0,0);
    31     speed = Ogre::Vector3(0,0,0);
    32     location = Ogre::Vector3(0,0,0);
    33     movable = true;
    34   }
     24    //! default constructor
     25    Element() {
     26      acceleration = Vector3(0,0,0);
     27      speed = Vector3(0,0,0);
     28      location = Vector3(0,0,0);
     29      movable = true;
     30    }
    3531
    36   /** constructor
    37    *  @param location_ sets locationvector of the element
    38    *  @param speed_ sets speedvector of the element
    39    *  @param acceleration_ sets accelerationvector of the element
    40    *  @param movable_ sets movability of the element
    41    */
    42   Element(Ogre::Vector3 location_, Ogre::Vector3 speed_, Ogre::Vector3 acceleration_, bool movable_) {
    43     acceleration = acceleration_;
    44     speed = speed_;
    45     location = location_;
    46     movable = movable_;
    47   }
     32    /** constructor
     33    *  @param location_ sets locationvector of the element
     34    *  @param speed_ sets speedvector of the element
     35    *  @param acceleration_ sets accelerationvector of the element
     36    *  @param movable_ sets movability of the element
     37    */
     38    Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
     39      acceleration = acceleration_;
     40      speed = speed_;
     41      location = location_;
     42      movable = movable_;
     43    }
    4844
    49   //! function to chance values of an element
    50   void setValues(Ogre::Vector3 location_, Ogre::Vector3 speed_, Ogre::Vector3 acceleration_, bool movable_) {
    51     acceleration = acceleration_;
    52     speed = speed_;
    53     location = location_;
    54     movable = movable_;
    55   }
     45    //! function to chance values of an element
     46    void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
     47      acceleration = acceleration_;
     48      speed = speed_;
     49      location = location_;
     50      movable = movable_;
     51    }
    5652
    57   /** calculates the distance between the element and an other point given by temp
    58    * @param e remote object to calculate distance to
    59    */
    60   float getDistance(Element e) {
    61     Ogre::Vector3 distance = e.location - location;
    62     return distance.length();
    63   }
     53    /** calculates the distance between the element and an other point given by temp
     54    * @param e remote object to calculate distance to
     55    */
     56    float getDistance(Element e) {
     57      Vector3 distance = e.location - location;
     58      return distance.length();
     59    }
    6460
    65   //! updates the data of an element
    66   void update(Element arrayOfElements[]) {
    67     if (this->movable == true) {calculateAcceleration(arrayOfElements);} //if element is movable, calculate acceleration
    68   }
     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
     64    }
    6965
    70   //! calculates the new acceleration of an element
    71   void calculateAcceleration(Element arrayOfElements[]) {
    72     acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);  //acceleration consisting of flocking-functions
    73   }
     66    //! calculates the new acceleration of an element
     67    void calculateAcceleration(Element arrayOfElements[]) {
     68      acceleration = separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements);  //acceleration consisting of flocking-functions
     69    }
    7470
    75   //! separation-function (keep elements separated, avoid crashs)
    76   Ogre::Vector3 separation(Element arrayOfElements[]) {
    77     using namespace Ogre;
    78     Vector3 steering = Vector3(0,0,0); //steeringvector
    79     Vector3 inverseDistance = Vector3(0,0,0);  //vector pointing away from possible collisions
    80     int numberOfNeighbour = 0;  //number of observed neighbours
    81     float distance = 0;  // distance to the actual element
    82     for(int i=0; i<ANZELEMENTS; i++) {  //go through all elements
    83       Element actual = arrayOfElements[i];  //get the actual element
    84       distance = getDistance(actual);  //get distance between this and actual
    85       if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
    86         inverseDistance = (0,0,0);
    87         inverseDistance = location-actual.location;  //calculate the distancevector heading towards this
    88         //adaptation of the inverseDistance to the distance
    89         if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
    90         if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;}
    91         if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;}
    92         if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;}
    93         steering = steering + inverseDistance;  //add up all significant steeringvectors
    94         numberOfNeighbour++;  //counts the elements inside the detectionradius
     71    //! separation-function (keep elements separated, avoid crashs)
     72    Vector3 separation(Element arrayOfElements[]) {
     73      using namespace Ogre;
     74      Vector3 steering = Vector3(0,0,0); //steeringvector
     75      Vector3 inverseDistance = Vector3(0,0,0);  //vector pointing away from possible collisions
     76      int numberOfNeighbour = 0;  //number of observed neighbours
     77      float distance = 0;  // distance to the actual element
     78      for(int i=0; i<ANZELEMENTS; i++) {  //go through all elements
     79        Element actual = arrayOfElements[i];  //get the actual element
     80        distance = getDistance(actual);  //get distance between this and actual
     81        if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
     82          inverseDistance = (0,0,0);
     83          inverseDistance = location-actual.location;  //calculate the distancevector heading towards this
     84          //adaptation of the inverseDistance to the distance
     85          if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
     86          if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;}
     87          if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;}
     88          if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;}
     89          steering = steering + inverseDistance;  //add up all significant steeringvectors
     90          numberOfNeighbour++;  //counts the elements inside the detectionradius
     91        }
    9592      }
     93      if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
     94      return steering;
    9695    }
    97     if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
    98     return steering;
    99   }
    10096
    101   //! alignment-function (lead elements to the same heading)
    102   Ogre::Vector3 alignment(Element arrayOfElements[]) {
    103     using namespace Ogre;
    104     Vector3 steering = Vector3(0,0,0); //steeringvector
    105     int numberOfNeighbour = 0;  //number of observed neighbours
    106     float distance = 0;
    107     //go through all elements
    108     for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
    109       Element actual = arrayOfElements[i];  //get the actual element
    110       float distance = getDistance(actual);  //get distance between this and actual
    111       if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
    112         steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
    113         numberOfNeighbour++;  //counts the elements inside the detectionradius
     97    //! alignment-function (lead elements to the same heading)
     98    Vector3 alignment(Element arrayOfElements[]) {
     99      using namespace Ogre;
     100      Vector3 steering = Vector3(0,0,0); //steeringvector
     101      int numberOfNeighbour = 0;  //number of observed neighbours
     102      float distance = 0;
     103      //go through all elements
     104      for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
     105        Element actual = arrayOfElements[i];  //get the actual element
     106        float distance = getDistance(actual);  //get distance between this and actual
     107        if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
     108          steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
     109          numberOfNeighbour++;  //counts the elements inside the detectionradius
     110        }
    114111      }
     112      if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
     113      return steering;
    115114    }
    116     if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
    117     return steering;
    118   }
    119115
    120   //! cohseion-function (keep elements close to each other)
    121   Ogre::Vector3 cohesion(Element arrayOfElements[]) {
    122     using namespace Ogre;
    123     Vector3 steering = Vector3(0,0,0); //steeringvector
    124     int numberOfNeighbour = 0;  //number of observed neighbours
    125     float distance = 0;
    126     //go through all elements
    127     for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
    128       Element actual = arrayOfElements[i];  //get the actual element
    129       float distance = getDistance(actual);  //get distance between this and actual
    130       if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
    131         steering = steering + actual.location;  //add up all locations of elements inside the detectionradius
    132         numberOfNeighbour++;  //counts the elements inside the detectionradius
     116    //! cohseion-function (keep elements close to each other)
     117    Vector3 cohesion(Element arrayOfElements[]) {
     118      using namespace Ogre;
     119      Vector3 steering = 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<ANZELEMENTS; 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        if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
     127          steering = steering + actual.location;  //add up all locations of elements inside the detectionradius
     128          numberOfNeighbour++;  //counts the elements inside the detectionradius
     129        }
    133130      }
     131      if(numberOfNeighbour > 0) {
     132        steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
     133        steering = steering - this->location;  //transform the vector for the ship
     134      }
     135      return steering;
    134136    }
    135     if(numberOfNeighbour > 0) {
    136       steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
    137       steering = steering - this->location;  //transform the vector for the ship
    138     }
    139     return steering;
    140   }
    141 };     //End of class Element
     137  };     //End of class Element
     138}
    142139
    143140#endif /* _Flocking_H__*/
Note: See TracChangeset for help on using the changeset viewer.