Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1228 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
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
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 */
28
29#include "OrxonoxStableHeaders.h"
30#include "NPC.h"
31
32#include "core/CoreIncludes.h"
33
34namespace orxonox {
35
36  CreateFactory(NPC);
37
38  NPC::NPC()
39  {
40    RegisterObject(NPC);
41    movable_ = true;
42  }
43
44  NPC::~NPC()
45  {
46  }
47
48  void NPC::loadParams(TiXmlElement* xmlElem)
49  {
50    Model::loadParams(xmlElem);
51  }
52
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   */
66  float NPC::getDistance(WorldEntity* temp)
67  {
68    Vector3 distance = temp->getPosition() - this->getPosition();
69    return distance.length();
70  }
71
72  /**
73   * updates the data of an element
74   */
75  void NPC::update()
76  {
77
78    //if element is movable, calculate acceleration
79    if (this->movable_ == true) calculateAcceleration();
80
81  }
82
83  /**
84   * tick this NPC
85   */
86  void NPC::tick(float dt)
87  {
88    update();
89    this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt);
90    this->translate(this->getVelocity()*dt);
91    this->setAcceleration(Vector3(0,0,0));
92  }
93
94  /**
95   * calculates the new acceleration of an element
96   */
97  void NPC::calculateAcceleration()
98  {
99    //acceleration consisting of flocking-functions
100    this->setAcceleration(separation() + alignment() + cohesion());
101  }
102
103  /**
104   * separation-function (keep elements separated, avoid crashs)
105   */
106  Vector3 NPC::separation()
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
112    for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::start(); it; ++it) {  //go through all elements
113      distance = getDistance(*it);  //get distance between this and actual
114      if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
115        inverseDistance = Vector3(0,0,0);
116        inverseDistance = this->getPosition() - it->getPosition();  //calculate the distancevector heading towards this
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   */
133  Vector3 NPC::alignment()
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
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
141      if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
142        steering = steering + it->getVelocity();  //add up all speedvectors inside the detectionradius
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   */
153  Vector3 NPC::cohesion()
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
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
161      if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
162        steering = steering + it->getPosition();  //add up all locations of elements inside the detectionradius
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.