Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/NPC.cc @ 1162

Last change on this file since 1162 was 1056, checked in by landauf, 17 years ago

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 6.0 KB
RevLine 
[603]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[603]4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Benjamin Knecht, beni_at_orxonox.net
24 *   Co-authors:
25 *      ...
26 *
27 */
[784]28
[1039]29#include "OrxonoxStableHeaders.h"
30#include "NPC.h"
[603]31
[1055]32#include "core/CoreIncludes.h"
[603]33
34namespace orxonox {
35
[627]36  CreateFactory(NPC);
37
[603]38  NPC::NPC()
39  {
[627]40    RegisterObject(NPC);
[617]41    movable_ = true;
[603]42  }
43
44  NPC::~NPC()
45  {
46  }
47
[627]48  void NPC::loadParams(TiXmlElement* xmlElem)
49  {
50    Model::loadParams(xmlElem);
51  }
52
[617]53  /**
54   * function to chance values of an element
55   */
56  void NPC::setValues(Vector3 location, Vector3 speed, Vector3 acceleration, bool movable) {
57    this->setAcceleration(acceleration);
58    this->setVelocity(speed);
59    this->translate(location);
60    movable_ = movable;
61  }
62
63  /**
64   * calculates the distance between the element and an other point given by temp
65   */
[627]66  float NPC::getDistance(WorldEntity* temp)
[617]67  {
68    Vector3 distance = temp->getPosition() - this->getPosition();
69    return distance.length();
70  }
71
72  /**
73   * updates the data of an element
74   */
[619]75  void NPC::update()
[617]76  {
77
78    //if element is movable, calculate acceleration
[627]79    if (this->movable_ == true) calculateAcceleration();
[617]80
81  }
82
83  /**
[619]84   * tick this NPC
85   */
86  void NPC::tick(float dt)
87  {
[1021]88    update();
[627]89    this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt);
90    this->translate(this->getVelocity()*dt);
91    this->setAcceleration(Vector3(0,0,0));
[619]92  }
93
94  /**
[617]95   * calculates the new acceleration of an element
96   */
[627]97  void NPC::calculateAcceleration()
[617]98  {
99    //acceleration consisting of flocking-functions
[627]100    this->setAcceleration(separation() + alignment() + cohesion());
[617]101  }
102
103  /**
104   * separation-function (keep elements separated, avoid crashs)
105   */
[627]106  Vector3 NPC::separation()
[617]107  {
108    Vector3 steering = Vector3(0,0,0); //steeringvector
109    Vector3 inverseDistance = Vector3(0,0,0);  //vector pointing away from possible collisions
110    int numberOfNeighbour = 0;  //number of observed neighbours
111    float distance = 0;  // distance to the actual element
[627]112    for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::start(); it; ++it) {  //go through all elements
113      distance = getDistance(*it);  //get distance between this and actual
[617]114      if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
115        inverseDistance = Vector3(0,0,0);
[627]116        inverseDistance = this->getPosition() - it->getPosition();  //calculate the distancevector heading towards this
[617]117        //adaptation of the inverseDistance to the distance
118        if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
119        if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;}
120        if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;}
121        if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;}
122        steering = steering + inverseDistance;  //add up all significant steeringvectors
123        numberOfNeighbour++;  //counts the elements inside the detectionradius
124      }
125    }
126    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
127    return steering;
128  }
129
130  /**
131   * alignment-function (lead elements to the same heading)
132   */
[627]133  Vector3 NPC::alignment()
[617]134  {
135    Vector3 steering = Vector3(0,0,0); //steeringvector
136    int numberOfNeighbour = 0;  //number of observed neighbours
137    //float distance = 0;
138    //go through all elements
[627]139    for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) {  //just working with 3 elements at the moment
140      float distance = getDistance(*it);  //get distance between this and actual
[617]141      if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
[627]142        steering = steering + it->getVelocity();  //add up all speedvectors inside the detectionradius
[617]143        numberOfNeighbour++;  //counts the elements inside the detectionradius
144      }
145    }
146    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
147    return steering;
148  }
149
150  /**
151   * cohseion-function (keep elements close to each other)
152   */
[627]153  Vector3 NPC::cohesion()
[617]154  {
155    Vector3 steering = Vector3(0,0,0); //steeringvector
156    int numberOfNeighbour = 0;  //number of observed neighbours
157    //float distance = 0;
158    //go through all elements
[627]159    for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) {  //just working with 3 elements at the moment
160      float distance = getDistance(*it);  //get distance between this and actual
[617]161      if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
[627]162        steering = steering + it->getPosition();  //add up all locations of elements inside the detectionradius
[617]163        numberOfNeighbour++;  //counts the elements inside the detectionradius
164      }
165    }
166    if(numberOfNeighbour > 0) {
167      steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
168      steering = steering - this->getPosition();  //transform the vector for the ship
169    }
170    return steering;
171  }
172
173} // end of class NPC
Note: See TracBrowser for help on using the repository browser.