Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 233


Ignore:
Timestamp:
Nov 21, 2007, 3:49:34 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

    r212 r233  
    2323
    2424
    25   void element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
     25  Element(Vector3 location_, Vector3 speed_, Vector3 acceleration_) {
    2626    acceleration = acceleration_;
    2727    speed = speed_;
     
    3636
    3737//EINF[GEN DES ELEMENTS
    38   void update(/*Element übergeben*/) {
    39     calculateAcceleration(/*Elementübergabe*/);  //updates the acceleration
     38  void update(Element* arrayOfElements) {
     39    calculateAcceleration(arrayOfElements);  //updates the acceleration
    4040    calculateSpeed();  //updates the speed
    4141    calculateLocation();  //updates the location
     
    4343
    4444//EINF[GEN DES ELEMENTS
    45   void calculateAcceleration(/*Element übergeben*/) {
     45  void calculateAcceleration(Element* arrayOfElements) {
    4646  //calculates the accelerationvector based on the steeringvectors of
    4747  //separtion, alignment and cohesion.
    48   acceleration = acceleration + separation() + alignment() + cohesion();
     48  acceleration = acceleration + separation(arrayOfElements) + 2*alignment(arrayOfElements) + 2*cohesion(arrayOfElements);
    4949  }
    5050
    5151  void calculateSpeed() {
    5252  speed = speed + acceleration;
     53  //speed = speed.normalise();
    5354  }
    5455
     
    5859  }
    5960
    60   Vector3 separation() {
     61  Vector3 separation(Element* arrayOfElements) {
    6162    Vector3 steering; //steeringvector
    6263    int numberOfNeighbour;  //number of observed neighbours
     
    6465    for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
    6566      Element actual = arrayOfElements[i];  //get the actual element
    66       float distance = Element.getDistance(actual);  //get distance between this and actual
     67      float distance = getDistance(actual);  //get distance between this and actual
    6768//DUMMY SEPERATION DETECTION DISTANCE = 25
    68       if ((distance > 0) && (distance<25)) {  //do only if actual is inside detectionradius
     69      if ((distance > 0) && (distance<1)) {  //do only if actual is inside detectionradius
    6970        Vector3 inverseDistance = actual.location-location;  //calculate the distancevector heading towards this
    7071        inverseDistance = inverseDistance.normalise(); //does this work correctly?  //normalise the distancevector
    71         inverseDistance = inverseDistance/distance;  //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)
     72        inverseDistance = inverseDistance/*/distance;*/ ;  //devide distancevector by distance (the closer the bigger gets the distancevector -> steeringvector)
    7273        steering = steering + inverseDistance;  //add up all significant steeringvectors
    7374        numberOfNeighbour++;  //counts the elements inside the detectionradius
     
    8081  }
    8182
    82   Vector3 alignment() {
     83  Vector3 alignment(Element* arrayOfElements) {
    8384    Vector3 steering; //steeringvector
    8485    int numberOfNeighbour;  //number of observed neighbours
     
    8687    for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
    8788      Element actual = arrayOfElements[i];  //get the actual element
    88       float distance = Element.getDistance(actual);  //get distance between this and actual
     89      float distance = getDistance(actual);  //get distance between this and actual
    8990//DUMMY ALIGNMENT DETECTION DISTANCE = 50
    90       if ((distance > 0) && (distance<50)) {  //check if actual element is inside detectionradius
     91      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
    9192        steering = steering + actual.speed;  //add up all speedvectors inside the detectionradius
    9293        numberOfNeighbour++;  //counts the elements inside the detectionradius
     
    99100  }
    100101
    101   Vector3 cohesion() {
     102  Vector3 cohesion(Element* arrayOfElements) {
    102103    Vector3 steering; //steeringvector
    103104    int numberOfNeighbour;  //number of observed neighbours
     
    105106    for(int i=1; i<3; i++) {  //just working with 3 elements at the moment
    106107      Element actual = arrayOfElements[i];  //get the actual element
    107       float distance = Element.getDistance(actual);  //get distance between this and actual
     108      float distance = getDistance(actual);  //get distance between this and actual
    108109// DUMMY COHESION DETECTION DISTANCE = 50
    109       if ((distance > 0) && (distance<50)) {  //check if actual element is inside detectionradius
     110      if ((distance > 0) && (distance<1000)) {  //check if actual element is inside detectionradius
    110111        steering = steering + actual.location;  //add up all locations of elements inside the detectionradius
    111112        numberOfNeighbour++;  //counts the elements inside the detectionradius
  • code/branches/AI/src/orxonox.cc

    r212 r233  
    8282Vector3 ElementAccelerationArray[2];
    8383
     84Element* arrayOfElements[2];
    8485
    8586
     
    103104    void moving(const FrameEvent& evt) {
    104105      SceneManager *mgr = root_->getSceneManager("Default SceneManager");
    105       mgr->getSceneNode("HeadNode1")->yaw((Radian)10*evt.timeSinceLastFrame);
     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);
    106113    }
    107114
     
    216223      SceneManager *mgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
    217224      Camera *cam = mgr->createCamera("Camera");
    218       cam->setPosition(Vector3(0,0,500));
     225      cam->setPosition(Vector3(0,0,1000));
    219226      cam->lookAt(Vector3(0,0,0));
    220227      Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);
     
    269276
    270277    //declaration of the 3 Ogreheads
     278   //muss leider global sein.....
     279    //Element* arrayOfElements[2];
     280
    271281    void example() {
    272282    SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
     
    275285    Entity* ent2 = mgr->createEntity("Head2", "ogrehead.mesh");
    276286    Entity* ent3 = mgr->createEntity("Head3", "ogrehead.mesh");
    277     SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(0,0,0));
     287    SceneNode *node1 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode1", Vector3(0,100,0));
    278288    SceneNode *node2 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode2", Vector3(100,0,0));
    279289    SceneNode *node3 = mgr->getRootSceneNode()->createChildSceneNode("HeadNode3", Vector3(-100,0,0));
     
    281291    node2->attachObject(ent2);
    282292    node3->attachObject(ent3);
     293    //Camera* cam  = mgr->getCamera("Camera");
     294    //node1->attachObject(cam);
    283295    ElementLocationArray[0] = node1->getPosition();
    284296    ElementLocationArray[1] = node2->getPosition();
     
    290302    ElementAccelerationArray[1] = (0,0,0);
    291303    ElementAccelerationArray[2] = (0,0,0);
    292     for (int i=0; i<3; i++) {
    293       Element* arrayOfElements[i] = new element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] );
    294     }
     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] );
     307
     308
     309
     310
     311   /* for (int i=0; i<3; i++) {
     312      Element* arrayOfElements[i] = new Element( ElementLocationArray[i], ElementSpeedArray[i], ElementAccelerationArray[i] );
     313    } */
     314   /* for (int i=0; i<3; i++) {
     315    arrayOfElements[i]->update(arrayOfElements);
     316    }  */
    295317    }
    296318};
Note: See TracChangeset for help on using the changeset viewer.