Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1663 was 1625, checked in by rgrieder, 16 years ago

merged hud branch back to trunk

  • Property svn:eol-style set to native
File size: 6.1 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    movable_(true)
40  {
41    RegisterObject(NPC);
42    registerAllVariables();
43  }
44
45  NPC::~NPC()
46  {
47  }
48
49  /**
50   * function to chance values of an element
51   */
52  void NPC::setValues(Vector3 location, Vector3 speed, Vector3 acceleration, bool movable) {
53    this->setAcceleration(acceleration);
54    this->setVelocity(speed);
55    this->translate(location);
56    movable_ = movable;
57  }
58 
59  void NPC::registerAllVariables(){
60    Model::registerAllVariables();
61    registerVar(&movable_, sizeof(movable_), network::DATA);
62  }
63 
64
65  /**
66   * calculates the distance between the element and an other point given by temp
67   */
68  float NPC::getDistance(WorldEntity* temp)
69  {
70    Vector3 distance = temp->getPosition() - this->getPosition();
71    return distance.length();
72  }
73
74  /**
75   * updates the data of an element
76   */
77  void NPC::update()
78  {
79
80    //if element is movable, calculate acceleration
81    if (this->movable_ == true) calculateAcceleration();
82
83  }
84
85  /**
86   * tick this NPC
87   */
88  void NPC::tick(float dt)
89  {
90    update();
91    this->setVelocity(0.995*this->getVelocity() + this->getAcceleration()*dt);
92    this->translate(this->getVelocity()*dt);
93    this->setAcceleration(Vector3(0,0,0));
94  }
95
96  /**
97   * calculates the new acceleration of an element
98   */
99  void NPC::calculateAcceleration()
100  {
101    //acceleration consisting of flocking-functions
102    this->setAcceleration(separation() + alignment() + cohesion());
103  }
104
105  /**
106   * separation-function (keep elements separated, avoid crashs)
107   */
108  Vector3 NPC::separation()
109  {
110    Vector3 steering = Vector3(0,0,0); //steeringvector
111    Vector3 inverseDistance = Vector3(0,0,0);  //vector pointing away from possible collisions
112    int numberOfNeighbour = 0;  //number of observed neighbours
113    float distance = 0;  // distance to the actual element
114    for(Iterator<WorldEntity> it = ObjectList<WorldEntity>::start(); it; ++it) {  //go through all elements
115      distance = getDistance(*it);  //get distance between this and actual
116      if ((distance > 0) && (distance < SEPERATIONDISTANCE)) {  //do only if actual is inside detectionradius
117        inverseDistance = Vector3(0,0,0);
118        inverseDistance = this->getPosition() - it->getPosition();  //calculate the distancevector heading towards this
119        //adaptation of the inverseDistance to the distance
120        if ((distance < 200) && (distance >= 120)) {inverseDistance = 2*inverseDistance;}
121        if ((distance < 120) && (distance >= 80)) {inverseDistance = 5*inverseDistance;}
122        if ((distance < 80) && (distance >= 40)) {inverseDistance = 10*inverseDistance;}
123        if ((distance < 40) && (distance > 0)) {inverseDistance = 10*inverseDistance;}
124        steering = steering + inverseDistance;  //add up all significant steeringvectors
125        numberOfNeighbour++;  //counts the elements inside the detectionradius
126      }
127    }
128    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> separation steeringvector
129    return steering;
130  }
131
132  /**
133   * alignment-function (lead elements to the same heading)
134   */
135  Vector3 NPC::alignment()
136  {
137    Vector3 steering = Vector3(0,0,0); //steeringvector
138    int numberOfNeighbour = 0;  //number of observed neighbours
139    //float distance = 0;
140    //go through all elements
141    for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) {  //just working with 3 elements at the moment
142      float distance = getDistance(*it);  //get distance between this and actual
143      if ((distance > 0) && (distance < ALIGNMENTDISTANCE)) {  //check if actual element is inside detectionradius
144        steering = steering + it->getVelocity();  //add up all speedvectors inside the detectionradius
145        numberOfNeighbour++;  //counts the elements inside the detectionradius
146      }
147    }
148    if(numberOfNeighbour > 0) { steering = steering / (float)numberOfNeighbour; }  //devide the sum of steeringvectors by the number of elements -> alignment steeringvector
149    return steering;
150  }
151
152  /**
153   * cohseion-function (keep elements close to each other)
154   */
155  Vector3 NPC::cohesion()
156  {
157    Vector3 steering = Vector3(0,0,0); //steeringvector
158    int numberOfNeighbour = 0;  //number of observed neighbours
159    //float distance = 0;
160    //go through all elements
161    for(Iterator<NPC> it = ObjectList<NPC>::start(); it; ++it) {  //just working with 3 elements at the moment
162      float distance = getDistance(*it);  //get distance between this and actual
163      if ((distance > 0) && (distance < COHESIONDISTANCE)) {  //check if actual element is inside detectionradius
164        steering = steering + it->getPosition();  //add up all locations of elements inside the detectionradius
165        numberOfNeighbour++;  //counts the elements inside the detectionradius
166      }
167    }
168    if(numberOfNeighbour > 0) {
169      steering = steering  / (float)numberOfNeighbour;  //devide the sum steeringvector by the number of elements -> cohesion steeringvector
170      steering = steering - this->getPosition();  //transform the vector for the ship
171    }
172    return steering;
173  }
174
175} // end of class NPC
Note: See TracBrowser for help on using the repository browser.