-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_particle.cpp
411 lines (342 loc) · 10.2 KB
/
agent_particle.cpp
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*********************************************************************
* Real-time physics simulation project
* Copyright (C) 2018-2019 Lluís Alemany Puig
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact: Lluís Alemany Puig ([email protected])
*
********************************************************************/
#include <physim/particles/agent_particle.hpp>
// C++ includes
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
// physim includes
#include <physim/math/private/math3.hpp>
#include <physim/geometry/rectangle.hpp>
#define FULL_MASK static_cast<int>(0xffffffff)
#define ninety static_cast<float>(M_PI)/2.0f
namespace physim {
using namespace math;
using namespace geometric;
namespace particles {
// PRIVATE
void agent_particle::partial_init() {
__pm3_assign_s(target, 0.0f);
__pm3_assign_s(orientation, 0.0f);
behaviour = agent_behaviour_type::none;
max_speed = 1.0f;
max_force = 1.0f;
const float w = 1.0f/7.0f;
align_weight = w;
seek_weight = w;
flee_weight = w;
arrival_weight = w;
arrival_distance = 0.0f;
coll_weight = w;
coll_distance = 5.0f;
ucoll_weight = w;
ucoll_distance = 5.0f;
wow_weight = w;
wow_distance = 5.0f;
}
// PUBLIC
agent_particle::agent_particle() {
partial_init();
}
agent_particle::agent_particle(const agent_particle& p) : sized_particle(p) {
__pm3_assign_v(target, p.target);
__pm3_assign_v(orientation, p.orientation);
behaviour = p.behaviour;
max_speed = p.max_speed;
max_force = p.max_force;
align_weight = p.align_weight;
seek_weight = p.seek_weight;
flee_weight = p.flee_weight;
arrival_weight = p.arrival_weight;
arrival_distance = p.arrival_distance;
coll_weight = p.coll_weight;
coll_distance = p.coll_distance;
ucoll_weight = p.ucoll_weight;
ucoll_distance = p.ucoll_distance;
wow_weight = p.wow_weight;
wow_distance = p.wow_distance;
}
agent_particle::~agent_particle() {
}
void agent_particle::init() {
base_particle::init();
partial_init();
}
// GETTERS
particle_type agent_particle::get_particle_type() const {
return particle_type::agent_particle;
}
bool agent_particle::is_behaviour_set(const agent_behaviour_type& b) const {
int k = static_cast<int>(behaviour) & static_cast<int>(b);
return k != 0;
}
// SETTERS
void agent_particle::set_behaviour(const agent_behaviour_type& b) {
int k = static_cast<int>(behaviour) | static_cast<int>(b);
behaviour = static_cast<agent_behaviour_type>(k);
}
void agent_particle::unset_behaviour(const agent_behaviour_type& b) {
int k = static_cast<int>(behaviour) & (FULL_MASK - static_cast<int>(b));
behaviour = static_cast<agent_behaviour_type>(k);
}
void agent_particle::unset_all_behaviours() {
behaviour = agent_behaviour_type::none;
}
// OTHERS
void agent_particle::apply_behaviours(vec3& weighted_steering) const {
if (behaviour == agent_behaviour_type::none) {
return;
}
vec3 v;
if (is_behaviour_set(agent_behaviour_type::seek)) {
seek_behaviour(v);
__pm3_add_acc_v(weighted_steering, v);
}
if (is_behaviour_set(agent_behaviour_type::flee)) {
flee_behaviour(v);
__pm3_add_acc_v(weighted_steering, v);
}
if (is_behaviour_set(agent_behaviour_type::arrival)) {
arrival_behaviour(v);
__pm3_add_acc_v(weighted_steering, v);
}
}
void agent_particle::apply_behaviours
(const std::vector<geometric::geometry *>& scene, vec3& weighted_steering)
const
{
if (behaviour == agent_behaviour_type::none) {
return;
}
vec3 v;
if (is_behaviour_set(agent_behaviour_type::collision_avoidance)) {
collision_avoidance_behaviour(scene, v);
__pm3_add_acc_v(weighted_steering, v);
}
}
void agent_particle::apply_behaviours
(const std::vector<agent_particle>& agents, vec3& weighted_steering)
const
{
if (behaviour == agent_behaviour_type::none) {
return;
}
vec3 v;
if (is_behaviour_set(agent_behaviour_type::unaligned_collision_avoidance)) {
unaligned_collision_avoidance_behaviour(agents, v);
__pm3_add_acc_v(weighted_steering, v);
}
if (is_behaviour_set(agent_behaviour_type::walk_with_me)) {
wwm_behaviour(agents, v);
__pm3_add_acc_v(weighted_steering, v);
}
}
/* steering behaviours */
void agent_particle::seek_behaviour(vec3& v) const {
vec3 des_vel;
__pm3_sub_v_v(des_vel, target, cur_pos);
normalise(des_vel, des_vel);
__pm3_mul_acc_s(des_vel, max_speed);
/*
vec3 steer;
__pm3_sub_v_v(steer, des_vel, cur_vel);
__pm3_assign_vs(v, steer, seek_weight);
*/
__pm3_sub_v_v_mul_s(v, des_vel, cur_vel, seek_weight);
}
void agent_particle::flee_behaviour(vec3& v) const {
vec3 des_vel;
__pm3_sub_v_v(des_vel, cur_pos, target);
normalise(des_vel, des_vel);
__pm3_mul_acc_s(des_vel, max_speed);
/*
vec3 steer;
__pm3_sub_v_v(steer, des_vel, cur_vel);
__pm3_assign_vs(v, steer, flee_weight);
*/
__pm3_sub_v_v_mul_s(v, des_vel, cur_vel, flee_weight);
}
void agent_particle::arrival_behaviour(vec3& v) const {
vec3 offset_target;
__pm3_sub_v_v(offset_target, target, cur_pos);
float dist_target = __pm3_norm(offset_target);
float ramped_distance = (dist_target/arrival_distance)*max_speed;
float clipped_speed = std::min(ramped_distance, max_speed);
vec3 des_vel;
__pm3_assign_vs(des_vel, offset_target, clipped_speed/dist_target);
/*
vec3 steer;
__pm3_sub_v_v(steer, des_vel, cur_vel);
__pm3_assign_vs(v, steer, arrival_weight);
*/
__pm3_sub_v_v_mul_s(v, des_vel, cur_vel, arrival_weight);
}
void agent_particle::collision_avoidance_behaviour
(const vector<geometry *>& scene, vec3& v)
const
{
__pm3_assign_s(v, 0.0f);
for (const geometry *g : scene) {
// skip distant objects
vec3 geom_pos = g->get_box_center();
float dist2;
if (g->get_geom_type() == geometry_type::Rectangle) {
// consider distance to object
const rectangle *r = static_cast<const rectangle *>(g);
dist2 = r->distance(cur_pos);
dist2 = dist2*dist2;
}
else if (g->get_geom_type() == geometry_type::Plane) {
// consider distance to object
const plane *p = static_cast<const plane *>(g);
dist2 = p->dist_point_plane(cur_pos);
dist2 = dist2*dist2;
}
else {
dist2 = __pm3_dist2(cur_pos, geom_pos) - g->approx_radius();
}
if (dist2 > coll_distance*coll_distance) {
continue;
}
bool skip = false;
// decide, using a second criteria, whether
// this geometry should be skipped or not
vec3 repulsion;
if (g->get_geom_type() == geometry_type::Rectangle) {
// this is a wall
const plane& p = static_cast<const rectangle *>(g)->get_plane();
__pm3_assign_v(repulsion, p.get_normal());
if (p.dist_point_plane(cur_pos) < 0.0f) {
__pm3_invert(repulsion, repulsion);
}
float angle = __pm3_angle(repulsion, cur_vel);
if (angle < 1.571f) {
// the '< 1.571' is counterintuitive, but just
// do a little drawing and you will know why
skip = true;
}
}
else if (g->get_geom_type() == geometry_type::Plane) {
// this is a wall
const plane *p = static_cast<const plane *>(g);
__pm3_assign_v(repulsion, p->get_normal());
if (p->dist_point_plane(cur_pos) < 0.0f) {
__pm3_invert(repulsion, repulsion);
}
float angle = __pm3_angle(repulsion, cur_vel);
if (angle < 1.571f) {
// the '< 1.571' is counterintuitive, but just
// do a little drawing and you will know why
skip = true;
}
}
else {
// project 'geom_pos' onto line through current
// position and director vector current velocity
vec3 agent_to_object = geom_pos - cur_pos;
float angle = __pm3_angle(agent_to_object, cur_vel);
if (angle > 1.571f) {
skip = true;
}
// use cross products to obtain repulsion vector
// (only if not skip)
if (not skip) {
vec3 c;
__pm3_cross(c, cur_vel, agent_to_object);
__pm3_cross(repulsion, cur_vel, c);
normalise(repulsion, repulsion);
}
}
if (skip) {
continue;
}
// compute contribution
__pm3_add_acc_vs(v, repulsion, coll_weight);
}
}
#define cosangle3d(u,v) __pm3_dot(u,v)/(__pm3_norm(u)*__pm3_norm(v))
#define angle3d(u,v) std::acos(cosangle3d(u,v))
void agent_particle::unaligned_collision_avoidance_behaviour
(const vector<agent_particle>& agents, vec3& v) const
{
vec3 X;
vec3 pos_rep, vel_rep;
__pm3_assign_s(v, 0.0f);
float r = ucoll_distance;
for (const agent_particle& a : agents) {
if (index == a.index) {
continue;
}
float D = __pm3_dist(cur_pos, a.cur_pos);
if (D - (R + a.R) > r) {
continue;
}
vec3 future_agent;
__pm3_add_v_vs(future_agent, a.cur_pos, a.cur_vel, 0.01f);
vec3 A_to_future;
__pm3_sub_v_v(A_to_future, future_agent, cur_pos);
float pred_angle = angle3d(cur_vel, A_to_future);
if (pred_angle > ninety) {
continue;
}
// vector from this agent to the other agent
vec3 AB;
__pm3_sub_v_v(AB, a.cur_pos, cur_pos);
float vta_angle = angle3d(AB, cur_vel);
if (vta_angle > ninety) {
continue;
}
// compute repulsion using vector from this position
// to the agent's position and this velocity
__pm3_cross(X, AB, cur_vel);
__pm3_cross(pos_rep, X,cur_vel);
// Do not normalise! The greater the velocity
// the greater the force.
__pm3_add_acc_vs(v, pos_rep, ucoll_weight);
}
}
void agent_particle::wwm_behaviour
(const std::vector<agent_particle>& agents, math::vec3& v) const
{
vec3 X;
vec3 pos_rep, vel_rep;
__pm3_assign_s(v, 0.0f);
float r = wow_distance;
for (const agent_particle& a : agents) {
if (index == a.index) {
continue;
}
float D = __pm3_dist(cur_pos, a.cur_pos);
if (D - (R + a.R) > r) {
continue;
}
// compute repulsion using vector from this position
// to the agent's position and this velocity
__pm3_cross(X, cur_vel, a.cur_vel);
__pm3_cross(pos_rep, X,cur_vel);
// Do not normalise! The greater the velocity
// the greater the force.
__pm3_add_acc_vs(v, pos_rep, wow_weight);
}
}
} // -- namespace particles
} // -- namespace physim