Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 325 for code/branches/AI


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

added Flocking

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
    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
  • code/branches/AI/src/orxonox.cc

    r233 r325  
    7878//my-stuff
    7979//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];
     80Vector3 ElementLocationArray[3];
     81Vector3 ElementSpeedArray[3];
     82Vector3 ElementAccelerationArray[3];
     83
     84Element arrayOfElements[3];
    8585
    8686
     
    104104    void moving(const FrameEvent& evt) {
    105105      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);
    113120    }
    114121
     
    291298    node2->attachObject(ent2);
    292299    node3->attachObject(ent3);
    293     //Camera* cam  = mgr->getCamera("Camera");
    294     //node1->attachObject(cam);
    295300    ElementLocationArray[0] = node1->getPosition();
    296301    ElementLocationArray[1] = node2->getPosition();
     
    302307    ElementAccelerationArray[1] = (0,0,0);
    303308    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] );
    307312
    308313
Note: See TracChangeset for help on using the changeset viewer.