Army ant simulation
Demo.h
Go to the documentation of this file.
1 
14 #ifndef DEMO_H_
15 #define DEMO_H_
16 #include "Box2D/Box2D.h"
17 #include "SFML/Graphics.hpp"
18 #include "SFML/Window.hpp"
19 #include <iostream>
20 #include "Robot.h"
21 #include "RobotController.h"
22 #include "Vterrain.h"
23 #include "V2BLTerrain.h"
24 #include "Ramp.h"
25 #include "VStepper.h"
26 #include <iostream>
27 #include <fstream>
28 #include "Config.h"
29 
30 #include <random>
31 #include "BoxTerrain.h"
32 
36 class MyContactListener_v2 : public b2ContactListener
37 {
38 public:
39  MyContactListener_v2(RobotController& robotController):m_robotController(&robotController){};
40 
41  void BeginContact(b2Contact* contact){
42 // printf("contact \n");
43  m_robotController->findContactRobots(contact);
44  }
45  void EndContact(b2Contact* contact) {
46  }
47 
48 private:
49  RobotController* m_robotController;
50 };
51 
60 class Demo {
61 public:
62  Demo(b2World* m_world, config::sConfig cfg);
63  virtual ~Demo();
64 
71  bool addRobotWithDelay();
79  bool addRobotWithDistance();
80 
86  void createBridgeFile();
87 
96  void demoLoop();
97 
101  double getBridgeHeight();
102 
109 
114  double getNewPathLength();
115 
120  sf::RenderWindow* getWindow();
121 
125  void init();
126 
133  void takeScreenshot(bool draw, int step);
134 
139  void writeResultFile();
140 
146  void writeBridgeFile();
147 
148 private:
149  sf::RenderWindow window; //SFML window used to display the simulation
150 // b2Vec2 m_gravity = b2Vec2(0.f, 0.f);
151  RobotController m_robotController; //Robot controller containing the vector of the robots
152  double m_to_px; //Scale to do the conversion from real dimensions (m) to simulated ones (pixels). Usually obtained from the terrain
153  BoxTerrain m_terrain; //Terrain used to do the simulation, the object can be changed to either: Terrain, BoxTerrain, Vterrain, V2BLTerrain, VStepper
154  MyContactListener_v2* myContactListener; //Contact listener
155  double m_it = 0; //Iteration counter used to create the robots with a given delay between them
156  int m_nbRobots = 0; //Number of robots created
157  double m_elapsedTime = 0; // in seconds (real-time =/= from simulation time)
158  double m_elapsedTimeDissolution = 0; //Duration of the dissolution phase (s)
159  double m_elapsedTimeBridge = 0; //Duration of the bridge formation phase (s)
160  double m_length = 0; // Bridge length (Body length)
161  double m_height = 0; // Bridge height (Body length)
162  int m_currentIt = 0; // Current iteration
163  int m_nbRobotsInBridge = 0; // Number of robots involved in the bridge structure, ie the ones in bridge state + the ones stuck under the structure
164  int m_nbRobotsInBridgeState = 0; // Number of robots in the bridge state
165 
166  bool m_stacking = false; //Flag to indicate that the robots are stacking
167  bool m_stableBridge = false; //Flag to indicate that a stable bridge has been reached
168 
169  b2Vec2 m_startP; //Start point of the bridge (on the left side of the obstacle
170  b2Vec2 m_endP; //End point of the bridge (on the left side of the obstacle
171 
172  config::sConfig m_config ; //Configuration parameters (cf Config.h file)
173 
174 // sf::Texture m_tex;
175 
176  std::ofstream m_logFile; //Overall result file
177  std::ofstream m_bridgeFile; //Bridge file that describe the bridge formation
178 
182  // random device class instance, source of 'true' randomness for initializing random seed
183  std::random_device m_rd;
184  // Mersenne twister PRNG, initialized with seed from previous random device instance
185  std::mt19937 m_gen;
186  double m_seed;
187  double m_std_dev=0.25; //0.25
188  // std::default_random_engine gen;
189  std::normal_distribution<double> m_gauss_delay;
190 
191 protected:
192  b2World* m_world = nullptr; // pointer on the Box2D world
193 };
194 
195 
196 #endif /* DEMO_H_ */
RobotController getController()
Definition: Demo.cpp:344
void init()
Definition: Demo.cpp:86
double getBridgeHeight()
Definition: Demo.cpp:353
void findContactRobots(b2Contact *contact)
Implement the Robot class and define enum linked to a robot object: e_state and side.
sf::RenderWindow * getWindow()
Definition: Demo.cpp:348
Definition: Demo.h:60
Implementation of the Ramp class which inherit from the Terrain class.
bool addRobotWithDelay()
Definition: Demo.cpp:261
Implementation of the Configuration class used to parse the configuration file and the sConfig struct...
void writeBridgeFile()
Definition: Demo.cpp:556
void createBridgeFile()
Definition: Demo.cpp:550
bool addRobotWithDistance()
Definition: Demo.cpp:301
void demoLoop()
Definition: Demo.cpp:95
Implementation of the Vterrain class which inherit from the Terrain class.
Implementation of the V2BLTerrain class which inherit from the Terrain class.
Implement the RobotController class. This class act as a supervisor for the Robots.
Demo(b2World *m_world, config::sConfig cfg)
Definition: Demo.cpp:41
void takeScreenshot(bool draw, int step)
Definition: Demo.cpp:598
void writeResultFile()
Definition: Demo.cpp:446
double getNewPathLength()
Definition: Demo.cpp:390