Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/orxonox/objects/NPC.cc @ 1591

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

Again some heavy changes in ObjectList and Iterator:
there are now two types of iterators:

Iterator<ClassName> can iterate through any objectlist, either given by ObjectList<AnyClassName>::begin() or anyidentifier→getObjects()→begin(). Important note Iterator<ClassName> uses dynamic_cast.
And yes, it's possible to do this: Iterator<WorldEntity> it = ObjectList<SpaceShip>::begin()

ObjectList<ClassName>::iterator is the second iterator - it uses the ObjectList in a templated manner and therefore doesn't need dynamic_cast. But the only thing you can do is iterating through exactly the right ObjectList: ObjectList<ClassName>::iterator it = ObjectList<ClassName>::begin(). Anything else fails.

Those changes bring, at my system, something around +12% FPS compared with trunk and +25% FPS compared with the last revision of core3. Although I have to admit the FPS gain is only that high because iterating through objects is the main thing we're doing ingame right now. It would look totally different with physics, sound, AI, scripts, triggers and so on.

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