Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/NPC.cc @ 710

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