-
Notifications
You must be signed in to change notification settings - Fork 2
/
CrowdWorld.cpp
121 lines (96 loc) · 2.62 KB
/
CrowdWorld.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
#include "CrowdWorld.h"
CrowdWorld::CrowdWorld(){
Render::Render * r = Render::getInstance();
}
//adds the new CrowdObject(s) to the end of the vector
void CrowdWorld::createNewObject(Json::Value v){
std::string s = v["type"].asString();
std::string fa = "fallen agent";
std::string a = "attractor";
std::string w = "wall";
std::string ag = "agent";
std::string ob = "obstacle";
if (s.compare(w) == 0){
Wall::Wall* ws = twoWalls(v);
objectList.push_back( ws );
objectList.push_back( &(ws[1]) );
Render::Render * r = Render::getInstance();
r->drawThis(ws, "wall.mesh");
return;
}
if (s.compare(ag) == 0){
objectList.push_back( new Agent(v) );
}
else
objectList.push_back( new CrowdObject(v) );
}
CrowdWorld::CrowdWorld( Json::Value w ){
Render::Render * r = Render::getInstance();
//loading from file
int numAgents = w["agents"].size();
for(int i = 0; i < numAgents ; i++ ){
Agent::Agent * a = new Agent(w["agents"][i]);
agentList.push_back( a );
r->drawThis(a, a->getMesh());
}
int numObjects = w["objects"].size();
for(int i = 0; i < numObjects ; i++){
createNewObject( w["objects"][i] );
}
//end loading
}
CrowdWorld::~CrowdWorld(){
Render::Render * r = Render::getInstance();
r->destroyInstance();
}
//updates each agent with visibility and collision information
void CrowdWorld::updateAgents(){
for( std::vector<Agent *>::iterator a = agentList.begin();
a != agentList.end();
a++ ){
//contains some inneficiency
for( std::vector<Agent *>::iterator b = agentList.begin();
b != agentList.end();
b++ ){
if (a != b) {
(*a)->checkVisible(*b);
(*a)->checkCollide(*b);
}
}
for( std::vector<CrowdObject *>::iterator c = objectList.begin();
c != objectList.end();
c++ ){
(* a)->checkVisible(* c);
(* a)->checkCollide(* c);
}
}
}
//calcs forces for each agent
void CrowdWorld::calcForces(){
for( std::vector<Agent * >::iterator it = agentList.begin();
it != agentList.end();
it++ ){
(*it)->calculateForces();
}
}
//applies forces for each agent
void CrowdWorld::stepWorld( float deltaT ){
for( std::vector<Agent *>::iterator it = agentList.begin();
it != agentList.end();
it++ ){
(* it)->applyForces(deltaT);
(* it)->reset();
}
}
void CrowdWorld::print(){
for( std::vector<Agent *>::iterator it = agentList.begin();
it != agentList.end();
it++ ){
(* it)->print();
}
}
void CrowdWorld::render(){
Render::Render * r = Render::getInstance();
//requires a float, but that shouldn't affect anything
r->update(0.1);
}