Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 426 for code/branches


Ignore:
Timestamp:
Dec 5, 2007, 10:08:12 PM (17 years ago)
Author:
motth
Message:

added actual flocking code and two frameworks

Location:
code/branches/AI/src
Files:
2 added
2 edited

Legend:

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

    r326 r426  
    1313#include <Ogre.h>
    1414#include <OgreVector3.h>
     15
    1516
    1617#include <iostream>
     
    2930    Vector3 speed;  // speedvector of the element
    3031    Vector3 acceleration;  // accelerationvector of the element
     32    bool movable;  // movability of the element
    3133
    3234  Element() {
     
    3436    speed = (0,0,0);
    3537    location = (0,0,0);
     38    movable = true;
    3639  }
    3740
    38   Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
     41  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
    3942    acceleration = acceleration_;
    4043    speed = speed_;
    4144    location = location_;
     45    movable = movable_;
    4246  }
    4347
    44   void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
     48  void setValues(Vector3 location_, Vector3 speed_, Vector3 acceleration_, bool movable_) {
    4549    acceleration = acceleration_;
    4650    speed = speed_;
    4751    location = location_;
     52    movable = movable_;
    4853  }
    4954
     
    5459  }
    5560
    56 //EINF[GEN DES ELEMENTS
     61//EINFÜGEN DES ELEMENTS
    5762  void update(Element arrayOfElements[], const FrameEvent& time) {
    58     calculateAcceleration(arrayOfElements);  //updates the acceleration
    59     calculateSpeed(time);  //updates the speed
    60     calculateLocation(time);  //updates the location
     63      if (this->movable == true) {calculateAcceleration(arrayOfElements);}
     64
     65 /*   if (this->movable ==  true) {
     66      calculateAcceleration(arrayOfElements);  //updates the acceleration
     67      calculateSpeed(time);  //updates the speed
     68      calculateLocation(time);  //updates the location
     69    }   */
    6170  }
    6271
    63 //EINF[GEN DES ELEMENTS
     72//EINFÜGEN DES ELEMENTS
    6473  void calculateAcceleration(Element arrayOfElements[]) {
    6574  //calculates the accelerationvector based on the steeringvectors of
     
    7988  Vector3 separation(Element arrayOfElements[]) {
    8089    Vector3* steering = new Vector3(0,0,0); //steeringvector
     90    Vector3* inverseDistance = new Vector3(0,0,0);
    8191    int numberOfNeighbour = 0;  //number of observed neighbours
     92    float distance = 0;
    8293    //go through all elements
    83     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
     94    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
    8495      Element actual = arrayOfElements[i];  //get the actual element
    85       float distance = getDistance(actual);  //get distance between this and actual
     96      distance = getDistance(actual);  //get distance between this and actual
    8697//DUMMY SEPERATION DETECTION DISTANCE =100
    87       if ((distance > 0) && (distance<100)) {  //do only if actual is inside detectionradius
    88         Vector3 inverseDistance = actual.location-location;  //calculate the distancevector heading towards this
    89         inverseDistance = inverseDistance.normalise(); //does this work correctly?  //normalise the distancevector
    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
     98      if ((distance > 0) && (distance < 200)) {  //do only if actual is inside detectionradius
     99        *inverseDistance = (0,0,0);
     100        *inverseDistance = location-actual.location;  //calculate the distancevector heading towards this
     101        //*inverseDistance = inverseDistance->normalise(); //does this work correctly?  //normalise the distancevector
     102        if ((distance < 100) && (distance >= 80)) {*inverseDistance = *inverseDistance*2;}
     103        if ((distance < 80) && (distance >= 60)) {*inverseDistance = *inverseDistance*5;}
     104        if ((distance < 60) && (distance >= 40)) {*inverseDistance = *inverseDistance*10;}
     105        if ((distance < 40) && (distance > 0)) {*inverseDistance = *inverseDistance*20;}
     106      //  *inverseDistance = *inverseDistance/distance;  //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)
     107        *steering = *steering + *inverseDistance;  //add up all significant steeringvectors
    92108        numberOfNeighbour++;  //counts the elements inside the detectionradius
    93109      }
     
    96112    *steering = *steering / (float)numberOfNeighbour;  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
    97113    }
     114    cout<<*steering<<endl;
    98115    return *steering;
    99116  }
     
    102119    Vector3* steering = new Vector3(0,0,0); //steeringvector
    103120    int numberOfNeighbour = 0;  //number of observed neighbours
     121    float distance = 0;
    104122    //go through all elements
    105     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
     123    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
    106124      Element actual = arrayOfElements[i];  //get the actual element
    107125      float distance = getDistance(actual);  //get distance between this and actual
    108126//DUMMY ALIGNMENT DETECTION DISTANCE = 1000
    109       if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
     127      if ((distance > 0) && (distance < 300)) {  //check if actual element is inside detectionradius
    110128        *steering = *steering + actual.speed;  //add up all speedvectors inside the detectionradius
    111129        numberOfNeighbour++;  //counts the elements inside the detectionradius
     
    121139    Vector3* steering = new Vector3(0,0,0); //steeringvector
    122140    int numberOfNeighbour = 0;  //number of observed neighbours
     141    float distance = 0;
    123142    //go through all elements
    124     for(int i=0; i<3; i++) {  //just working with 3 elements at the moment
     143    for(int i=0; i<9; i++) {  //just working with 3 elements at the moment
    125144      Element actual = arrayOfElements[i];  //get the actual element
    126145      float distance = getDistance(actual);  //get distance between this and actual
    127146// DUMMY COHESION DETECTION DISTANCE = 1000
    128       if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
     147      if ((distance > 0) && (distance < 5000)) {  //check if actual element is inside detectionradius
    129148        *steering = *steering + actual.location;  //add up all locations of elements inside the detectionradius
    130149        numberOfNeighbour++;  //counts the elements inside the detectionradius
     
    133152    if(numberOfNeighbour > 0) {
    134153    *steering = *steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
     154    *steering = *steering - this->location;  // (?) Koordinatensystem?
    135155    }
    136156    return *steering;
    137157  }
    138  
    139158};
    140159
  • code/branches/AI/src/orxonox.cc

    r325 r426  
    3535#include <CEGUI/CEGUI.h>
    3636#include <OgreCEGUIRenderer.h>
     37#include <OgreMath.h>
    3738
    3839#include <string>
     
    4243#include "loader/LevelLoader.h"
    4344#include "Flocking.h"
     45#include "AIClass.h"
    4446
    4547// some tests to see if enet works without includsion
     
    7880//my-stuff
    7981//globale definition eines Arrays welches alle nodes enthält
    80 Vector3 ElementLocationArray[3];
    81 Vector3 ElementSpeedArray[3];
    82 Vector3 ElementAccelerationArray[3];
    83 
    84 Element arrayOfElements[3];
     82Vector3 ElementLocationArray[9];
     83Vector3 ElementSpeedArray[9];
     84Vector3 ElementAccelerationArray[9];
     85
     86Element arrayOfElements[9];
     87
     88 // float time = 0;
    8589
    8690
     
    105109      SceneManager *mgr = root_->getSceneManager("Default SceneManager");
    106110
    107 
    108 
    109111      arrayOfElements[0].update(arrayOfElements, evt);
    110112      arrayOfElements[1].update(arrayOfElements, evt);
    111113      arrayOfElements[2].update(arrayOfElements, evt);
     114      arrayOfElements[3].update(arrayOfElements, evt);
     115      arrayOfElements[4].update(arrayOfElements, evt);
     116      arrayOfElements[5].update(arrayOfElements, evt);
     117      arrayOfElements[6].update(arrayOfElements, evt);
     118      arrayOfElements[7].update(arrayOfElements, evt);
     119      arrayOfElements[8].update(arrayOfElements, evt);
     120
     121 /*   arrayOfElements[0].update(arrayOfElements, evt);
     122      arrayOfElements[1].update(arrayOfElements, evt);
     123      arrayOfElements[2].update(arrayOfElements, evt);
     124      arrayOfElements[3].update(arrayOfElements, evt);
     125      arrayOfElements[4].update(arrayOfElements, evt);   */
     126
     127
     128      for(int i=0; i<9; i++) {
     129
     130         arrayOfElements[i].speed = 0.995*arrayOfElements[i].speed + arrayOfElements[i].acceleration*evt.timeSinceLastFrame;
     131
     132         arrayOfElements[i].location = arrayOfElements[i].location + arrayOfElements[i].speed*evt.timeSinceLastFrame;
     133
     134         arrayOfElements[i].acceleration  = (0,0,0);
     135      }
    112136
    113137      mgr->getSceneNode("HeadNode1")->setPosition(arrayOfElements[0].location);
    114138      mgr->getSceneNode("HeadNode2")->setPosition(arrayOfElements[1].location);
    115139      mgr->getSceneNode("HeadNode3")->setPosition(arrayOfElements[2].location);
     140      mgr->getSceneNode("HeadNode4")->setPosition(arrayOfElements[3].location);
     141      mgr->getSceneNode("HeadNode5")->setPosition(arrayOfElements[4].location);
     142      mgr->getSceneNode("HeadNode6")->setPosition(arrayOfElements[5].location);
     143      mgr->getSceneNode("HeadNode7")->setPosition(arrayOfElements[6].location);
     144      mgr->getSceneNode("HeadNode8")->setPosition(arrayOfElements[7].location);
     145      mgr->getSceneNode("HeadNode9")->setPosition(arrayOfElements[8].location);
     146
     147
     148      /*
     149
     150      mgr->getSceneNode("HeadNode9")->setPosition(Vector3(200*cos(10*time),0,0));
     151      time = time + evt.timeSinceLastFrame;
     152
     153     */
    116154
    117155
     
    292330    Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh");
    293331    Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh");
    294     SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(0,100,0));
    295     SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(100,0,0));
    296     SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-100,0,0));
     332    Entity* ent4 = mgr->createEntity("Head4", "ogrehead.mesh");
     333    Entity* ent5 = mgr->createEntity("Head5", "ogrehead.mesh");
     334    Entity* ent6 = mgr->createEntity("Head6", "ogrehead.mesh");
     335    Entity* ent7 = mgr->createEntity("Head7", "ogrehead.mesh");
     336    Entity* ent8 = mgr->createEntity("Head8", "ogrehead.mesh");
     337    Entity* ent9 = mgr->createEntity("Head9", "ogrehead.mesh");
     338    SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(100,300,100));
     339    SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(300,0,200));
     340    SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-300,0,-100));
     341    SceneNode *node4 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode4", Vector3(-100,-300,150));
     342    SceneNode *node5 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode5", Vector3(150,150,-100));
     343    SceneNode *node6 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode6", Vector3(150,-150,-100));
     344    SceneNode *node7 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode7", Vector3(-150,-150,0));
     345    SceneNode *node8 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode8", Vector3(-150,150,0));
     346    SceneNode *node9 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode9", Vector3(0,0,0)); 
     347
     348// follwing camera
     349
     350 //  Camera *cam = mgr->getCamera("Camera");
     351 //  node1->attachObject(cam);
     352
     353
     354
     355
    297356    node1->attachObject(ent1);
    298357    node2->attachObject(ent2);
    299358    node3->attachObject(ent3);
     359    node4->attachObject(ent4);
     360    node5->attachObject(ent5);
     361    node6->attachObject(ent6);
     362    node7->attachObject(ent7);
     363    node8->attachObject(ent8);
     364    node9->attachObject(ent9);
    300365    ElementLocationArray[0] = node1->getPosition();
    301366    ElementLocationArray[1] = node2->getPosition();
    302367    ElementLocationArray[2] = node3->getPosition();
     368    ElementLocationArray[3] = node4->getPosition();
     369    ElementLocationArray[4] = node5->getPosition();
     370    ElementLocationArray[5] = node6->getPosition();
     371    ElementLocationArray[6] = node7->getPosition();
     372    ElementLocationArray[7] = node8->getPosition();
     373    ElementLocationArray[8] = node9->getPosition();
     374/*
     375ElementLocationArray[5] = node6->getPosition();
     376ElementLocationArray[6] = node7->getPosition();*/
    303377    ElementSpeedArray[0] = (0,0,0);
    304378    ElementSpeedArray[1] = (0,0,0);
    305379    ElementSpeedArray[2] = (0,0,0);
     380    ElementSpeedArray[3] = (0,0,0);
     381    ElementSpeedArray[4] = (0,0,0);
     382    ElementSpeedArray[5] = (0,0,0);
     383    ElementSpeedArray[6] = (0,0,0);
     384    ElementSpeedArray[7] = (0,0,0);
     385    ElementSpeedArray[8] = (0,0,0);
     386/*
     387ElementSpeedArray[5] = (0,0,0);
     388ElementSpeedArray[6] = (0,0,0); */
    306389    ElementAccelerationArray[0] = (0,0,0);
    307390    ElementAccelerationArray[1] = (0,0,0);
    308391    ElementAccelerationArray[2] = (0,0,0);
    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] );
     392    ElementAccelerationArray[3] = (0,0,0);
     393    ElementAccelerationArray[4] = (0,0,0);
     394    ElementAccelerationArray[5] = (0,0,0);
     395    ElementAccelerationArray[6] = (0,0,0);
     396    ElementAccelerationArray[7] = (0,0,0);
     397    ElementAccelerationArray[8] = (0,0,0);
     398/*
     399ElementAccelerationArray[5] = (0,0,0);
     400ElementAccelerationArray[6] = (0,0,0); */
     401    arrayOfElements[0].setValues( ElementLocationArray[0], ElementSpeedArray[0], ElementAccelerationArray[0], true);
     402    arrayOfElements[1].setValues( ElementLocationArray[1], ElementSpeedArray[1], ElementAccelerationArray[1], true);
     403    arrayOfElements[2].setValues( ElementLocationArray[2], ElementSpeedArray[2], ElementAccelerationArray[2], true);
     404    arrayOfElements[3].setValues( ElementLocationArray[3], ElementSpeedArray[3], ElementAccelerationArray[3], true);
     405    arrayOfElements[4].setValues( ElementLocationArray[4], ElementSpeedArray[4], ElementAccelerationArray[4], true);
     406    arrayOfElements[5].setValues( ElementLocationArray[5], ElementSpeedArray[5], ElementAccelerationArray[5], true);
     407    arrayOfElements[6].setValues( ElementLocationArray[6], ElementSpeedArray[6], ElementAccelerationArray[6], true);
     408    arrayOfElements[7].setValues( ElementLocationArray[7], ElementSpeedArray[7], ElementAccelerationArray[7], true);
     409    arrayOfElements[8].setValues( ElementLocationArray[8], ElementSpeedArray[8], ElementAccelerationArray[8], false);
     410/*
     411arrayOfElements[5].setValues( ElementLocationArray[5], ElementSpeedArray[5], ElementAccelerationArray[5], false);
     412arrayOfElements[6].setValues( ElementLocationArray[6], ElementSpeedArray[6], ElementAccelerationArray[6], false);*/
    312413
    313414
     
    320421    arrayOfElements[i]->update(arrayOfElements);
    321422    }  */
     423
     424//testing AIPilot -> function steer
     425  //  AIPilot temp;
     426  //  Vector3 foo = temp.steer(Vector3(0,0,1));
     427
     428
    322429    }
    323430};
Note: See TracChangeset for help on using the changeset viewer.