Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 18, 2007, 10:11:27 PM (17 years ago)
Author:
bknecht
Message:

working AI added (put them aside if you want)

Location:
code/branches/FICN/src/orxonox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/FICN/src/orxonox/Orxonox.cc

    r616 r627  
    6363#include "objects/Tickable.h"
    6464#include "objects/Timer.h"
     65#include "objects/NPC.h"
    6566#include "core/ArgReader.h"
    6667#include "core/Factory.h"
     
    9091      {
    9192        auMan_->update();
     93        updateAI();
    9294
    9395        if(mode_==PRESENTATION)
     
    100102        mKeyboard->capture();
    101103        return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
     104      }
     105
     106      void updateAI()
     107      {
     108        for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it)
     109        {
     110          it->update();
     111        }
    102112      }
    103113
  • code/branches/FICN/src/orxonox/objects/NPC.cc

    r619 r627  
    2929#include "../core/Iterator.h"
    3030#include "../core/ObjectList.h"
    31 //#include "../Flocking.h"
    3231
    3332namespace orxonox {
    3433
     34  CreateFactory(NPC);
     35
    3536  NPC::NPC()
    3637  {
     38    RegisterObject(NPC);
    3739    movable_ = true;
    3840  }
     
    4042  NPC::~NPC()
    4143  {
     44  }
     45
     46  void NPC::loadParams(TiXmlElement* xmlElem)
     47  {
     48    Model::loadParams(xmlElem);
    4249  }
    4350
     
    5562   * calculates the distance between the element and an other point given by temp
    5663   */
    57   float NPC::getDistance(NPC* temp)
     64  float NPC::getDistance(WorldEntity* temp)
    5865  {
    5966    Vector3 distance = temp->getPosition() - this->getPosition();
     
    6774  {
    6875
    69     // find out about this arrayOfElements
    70     NPC* arrayOfElements[ANZELEMENTS];
    71 
    7276    //if element is movable, calculate acceleration
    73     if (this->movable_ == true) calculateAcceleration(arrayOfElements);
     77    if (this->movable_ == true) calculateAcceleration();
    7478
    7579  }
     
    8185  {
    8286
    83 
     87    this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt);
     88    this->translate(this->getVelocity()*dt);
     89    this->setAcceleration(Vector3(0,0,0));
    8490  }
    8591
     
    8793   * calculates the new acceleration of an element
    8894   */
    89   void NPC::calculateAcceleration(NPC** arrayOfElements)
     95  void NPC::calculateAcceleration()
    9096  {
    9197    //acceleration consisting of flocking-functions
    92     this->setAcceleration(separation(arrayOfElements) + alignment(arrayOfElements) + cohesion(arrayOfElements));
     98    this->setAcceleration(separation() + alignment() + cohesion());
    9399  }
    94100
     
    96102   * separation-function (keep elements separated, avoid crashs)
    97103   */
    98   Vector3 NPC::separation(NPC** arrayOfElements)
     104  Vector3 NPC::separation()
    99105  {
    100106    Vector3 steering = Vector3(0,0,0); //steeringvector
     
    102108    int numberOfNeighbour = 0;  //number of observed neighbours
    103109    float distance = 0;  // distance to the actual element
    104     for(int i=0; i<ANZELEMENTS; i++) {  //go through all elements
    105       NPC* actual = arrayOfElements[i];  //get the actual element
    106       distance = getDistance(actual);  //get distance between this and actual
     110    for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::start(); it; ++it) {  //go through all elements
     111      distance = getDistance(*it);  //get distance between this and actual
    107112      if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
    108113        inverseDistance = Vector3(0,0,0);
    109         inverseDistance = this->getPosition() - actual->getPosition();  //calculate the distancevector heading towards this
     114        inverseDistance = this->getPosition() - it->getPosition();  //calculate the distancevector heading towards this
    110115        //adaptation of the inverseDistance to the distance
    111116        if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
     
    124129   * alignment-function (lead elements to the same heading)
    125130   */
    126   Vector3 NPC::alignment(NPC** arrayOfElements)
     131  Vector3 NPC::alignment()
    127132  {
    128133    Vector3 steering = Vector3(0,0,0); //steeringvector
     
    130135    //float distance = 0;
    131136    //go through all elements
    132     for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
    133       NPC* actual = arrayOfElements[i];  //get the actual element
    134       float distance = getDistance(actual);  //get distance between this and actual
     137    for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) {  //just working with 3 elements at the moment
     138      float distance = getDistance(*it);  //get distance between this and actual
    135139      if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
    136         steering = steering + actual->getVelocity();  //add up all speedvectors inside the detectionradius
     140        steering = steering + it->getVelocity();  //add up all speedvectors inside the detectionradius
    137141        numberOfNeighbour++;  //counts the elements inside the detectionradius
    138142      }
     
    145149   * cohseion-function (keep elements close to each other)
    146150   */
    147   Vector3 NPC::cohesion(NPC** arrayOfElements)
     151  Vector3 NPC::cohesion()
    148152  {
    149153    Vector3 steering = Vector3(0,0,0); //steeringvector
     
    151155    //float distance = 0;
    152156    //go through all elements
    153     for(int i=0; i<ANZELEMENTS; i++) {  //just working with 3 elements at the moment
    154       NPC* actual = arrayOfElements[i];  //get the actual element
    155       float distance = getDistance(actual);  //get distance between this and actual
     157    for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) {  //just working with 3 elements at the moment
     158      float distance = getDistance(*it);  //get distance between this and actual
    156159      if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
    157         steering = steering + actual->getPosition();  //add up all locations of elements inside the detectionradius
     160        steering = steering + it->getPosition();  //add up all locations of elements inside the detectionradius
    158161        numberOfNeighbour++;  //counts the elements inside the detectionradius
    159162      }
  • code/branches/FICN/src/orxonox/objects/NPC.h

    r619 r627  
    99
    1010// includes
    11 #include "WorldEntity.h"
    1211#include "Model.h"
    1312
     
    2019      NPC();
    2120      virtual ~NPC();
     21      virtual void loadParams(TiXmlElement* xmlElem);
    2222      void tick(float dt);
    2323      void update();
     
    2525
    2626    private:
    27       float getDistance(NPC* temp);
    28       void calculateAcceleration(NPC** arrayOfElements);
    29       Vector3 separation(NPC** arrayOfElements);
    30       Vector3 alignment(NPC** arrayOfElements);
    31       Vector3 cohesion(NPC** arrayOfElements);
     27      float getDistance(WorldEntity* temp);
     28      void calculateAcceleration();
     29      Vector3 separation();
     30      Vector3 alignment();
     31      Vector3 cohesion();
    3232
    3333
Note: See TracChangeset for help on using the changeset viewer.