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