-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathAgent.cpp
More file actions
217 lines (163 loc) · 6.27 KB
/
Agent.cpp
File metadata and controls
217 lines (163 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <random>
#include "Agent.h"
using namespace std;
const float PI = 3.14159265359F;
int Agent::crowdIdx = -1;
default_random_engine generator;
Agent::Agent() {
id = ++crowdIdx;
radius = 0.2F;
// Desired Speed Based on (Moussaid et al., 2009)
normal_distribution<float> distribution(1.29F, 0.19F); // Generate random value of mean 1.29 and standard deviation 0.19
desiredSpeed = distribution(generator);
colour.set(0.0, 0.0, 0.0);
position.set(0.0, 0.0, 0.0);
velocity.set(0.0, 0.0, 0.0);
}
Agent::~Agent() {
path.clear(); // Remove waypoints
crowdIdx--;
}
void Agent::setRadius(float radius) {
this->radius = radius;
}
void Agent::setDesiredSpeed(float speed) {
desiredSpeed = speed;
}
void Agent::setColour(float red, float green, float blue) {
colour.set(red, green, blue);
}
void Agent::setPosition(float x, float y) {
position.set(x, y, 0.0);
}
void Agent::setPath(float x, float y, float radius) {
path.push_back({ Point3f(x, y, 0.0), radius });
}
Point3f Agent::getPath() {
Vector3f distanceCurr, distanceNext;
distanceCurr = path[0].position - position; // Distance to current waypoint
if (path.size() > 2) {
distanceNext = path[1].position - position; // Distance to next waypoint
// Set Next Waypoint as Current Waypoint if Next Waypoint is Nearer
if (distanceNext.lengthSquared() < distanceCurr.lengthSquared()) {
path.push_back(path.front());
path.pop_front();
distanceCurr = distanceNext;
}
}
// Move Front Point to Back if Within Radius
if (distanceCurr.lengthSquared() < (path.front().radius * path.front().radius)) {
path.push_back(path.front());
path.pop_front();
}
return path.front().position;
}
float Agent::getOrientation() {
return (atan2(velocity.y, velocity.x) * (180 / PI));
}
Point3f Agent::getAheadVector() const {
return (velocity + position);
}
void Agent::move(vector<Agent *> agents, vector<Wall *> walls, float stepTime) {
Vector3f acceleration;
// Compute Social Force
acceleration = drivingForce(getPath()) + agentInteractForce(agents) + wallInteractForce(walls);
// Compute New Velocity
velocity = velocity + acceleration * stepTime;
// Truncate Velocity if Exceed Maximum Speed (Magnitude)
if (velocity.lengthSquared() > (desiredSpeed * desiredSpeed)) {
velocity.normalize();
velocity *= desiredSpeed;
}
// Compute New Position
position = position + velocity * stepTime;
}
Vector3f Agent::drivingForce(const Point3f position_target) {
const float T = 0.54F; // Relaxation time based on (Moussaid et al., 2009)
Vector3f e_i, f_i;
// Compute Desired Direction
// Formula: e_i = (position_target - position_i) / ||(position_target - position_i)||
e_i = position_target - position;
e_i.normalize();
// Compute Driving Force
// Formula: f_i = ((desiredSpeed * e_i) - velocity_i) / T
f_i = ((desiredSpeed * e_i) - velocity) * (1 / T);
return f_i;
}
Vector3f Agent::agentInteractForce(vector<Agent *> agents) {
// Constant Values Based on (Moussaid et al., 2009)
const float lambda = 2.0; // Weight reflecting relative importance of velocity vector against position vector
const float gamma = 0.35F; // Speed interaction
const float n_prime = 3.0; // Angular interaction
const float n = 2.0; // Angular intaraction
const float A = 4.5; // Modal parameter A
Vector3f distance_ij, e_ij, D_ij, t_ij, n_ij, f_ij;
float B, theta, f_v, f_theta;
int K;
f_ij.set(0.0, 0.0, 0.0);
for (const Agent *agent_j : agents) {
// Do Not Compute Interaction Force to Itself
if (agent_j->id != id) {
// Compute Distance Between Agent j and i
distance_ij = agent_j->position - position;
// Skip Computation if Agents i and j are Too Far Away
if (distance_ij.lengthSquared() > (2.0 * 2.0))
continue;
// Compute Direction of Agent j from i
// Formula: e_ij = (position_j - position_i) / ||position_j - position_i||
e_ij = distance_ij;
e_ij.normalize();
// Compute Interaction Vector Between Agent i and j
// Formula: D = lambda * (velocity_i - velocity_j) + e_ij
D_ij = lambda * (velocity - agent_j->velocity) + e_ij;
// Compute Modal Parameter B
// Formula: B = gamma * ||D_ij||
B = gamma * D_ij.length();
// Compute Interaction Direction
// Formula: t_ij = D_ij / ||D_ij||
t_ij = D_ij;
t_ij.normalize();
// Compute Angle Between Interaction Direction (t_ij) and Vector Pointing from Agent i to j (e_ij)
theta = t_ij.angle(e_ij);
// Compute Sign of Angle 'theta'
// Formula: K = theta / |theta|
K = (theta == 0) ? 0 : static_cast<int>(theta / abs(theta));
// Compute Amount of Deceleration
// Formula: f_v = -A * exp(-distance_ij / B - ((n_prime * B * theta) * (n_prime * B * theta)))
f_v = -A * exp(-distance_ij.length() / B - ((n_prime * B * theta) * (n_prime * B * theta)));
// Compute Amount of Directional Changes
// Formula: f_theta = -A * K * exp(-distance_ij / B - ((n * B * theta) * (n * B * theta)))
f_theta = -A * K * exp(-distance_ij.length() / B - ((n * B * theta) * (n * B * theta)));
// Compute Normal Vector of Interaction Direction Oriented to the Left
n_ij.set(-t_ij.y, t_ij.x, 0.0);
// Compute Interaction Force
// Formula: f_ij = f_v * t_ij + f_theta * n_ij
f_ij += f_v * t_ij + f_theta * n_ij;
}
}
return f_ij;
}
Vector3f Agent::wallInteractForce(vector<Wall *> walls) {
//const float repulsionRange = 0.3F; // Repulsion range based on (Moussaid et al., 2009)
const int a = 3;
const float b = 0.1F;
Point3f nearestPoint;
Vector3f vector_wi, minVector_wi;
float distanceSquared, minDistanceSquared = INFINITY, d_w, f_iw;
for (Wall *wall : walls) {
nearestPoint = wall->getNearestPoint(position);
vector_wi = position - nearestPoint; // Vector from wall to agent i
distanceSquared = vector_wi.lengthSquared();
// Store Nearest Wall Distance
if (distanceSquared < minDistanceSquared) {
minDistanceSquared = distanceSquared;
minVector_wi = vector_wi;
}
}
d_w = sqrt(minDistanceSquared) - radius; // Distance between wall and agent i
// Compute Interaction Force
// Formula: f_iw = a * exp(-d_w / b)
f_iw = a * exp(-d_w / b);
minVector_wi.normalize();
return f_iw * minVector_wi;
}