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