Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/orxonox_tutorial/src/orxonox/objects/NPC.cc @ 2213

Last change on this file since 2213 was 1747, checked in by landauf, 16 years ago

merged core3 back to trunk

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