Army ant simulation
Vterrain.cpp
1 /*
2  * Vterrain.cpp
3  *
4  * Created on: 8 oct. 2018
5  * Author: lucie
6  */
7 
8 #include "Vterrain.h"
9 
10 #include "helpers.h"
11 
12 Vterrain::Vterrain(){
13 }
14 
15 Vterrain::Vterrain(b2World* world, sf::RenderWindow& window, config::sTerrain terrainParam, int WINDOW_X_PX, double bodyLength)
16 : Terrain(world, window, terrainParam, WINDOW_X_PX, bodyLength){
17  m_M_TO_PX = WINDOW_X_PX / (2*m_runaway+m_width);
18 
19 }
20 
21 Vterrain::~Vterrain() {
22  // TODO Auto-generated destructor stub
23 }
24 
25 void Vterrain::create(b2World* world, sf::RenderWindow& window, config::sTerrain terrainParam, int WINDOW_X_PX, double bodyLength){
26  Terrain::create(world, window, terrainParam, WINDOW_X_PX, bodyLength);
27  m_M_TO_PX = WINDOW_X_PX / (2*m_runaway+m_width);
28  printf("m_M_TO_PX: %f, \n", m_M_TO_PX);
29 }
30 
31 void Vterrain::createBody(b2World* world){
32 
33  b2BodyDef BodyDef;
34  BodyDef.position = b2Vec2(0, 0);
35  BodyDef.type = b2_staticBody;
36  m_groundBody = world->CreateBody(&BodyDef);
37 
38  b2EdgeShape edgeShape;
39 
40  b2FixtureDef firstR;
41  edgeShape.Set( b2Vec2(0,m_posY), b2Vec2(m_runaway,m_posY) );
42  firstR.shape = &edgeShape;
43  m_groundBody->CreateFixture(&firstR);
44 
45  b2FixtureDef firstSlope;
46  edgeShape.Set( b2Vec2(m_runaway,m_posY), b2Vec2(m_runaway+m_width/2,m_posY+m_height) );
47  firstSlope.shape = &edgeShape;
48  m_groundBody->CreateFixture(&firstSlope);
49 
50  b2FixtureDef secondSlope;
51  edgeShape.Set( b2Vec2(m_runaway+m_width/2,m_posY+m_height), b2Vec2(m_runaway+m_width,m_posY) );
52  secondSlope.shape = &edgeShape;
53  m_groundBody->CreateFixture(&secondSlope);
54 
55  b2FixtureDef endR;
56  edgeShape.Set( b2Vec2(m_runaway+m_width,m_posY), b2Vec2(4*m_runaway+m_width,m_posY) );
57  endR.shape = &edgeShape;
58  m_groundBody->CreateFixture(&endR);
59 
60 //
61 // b2BodyDef BodyDef;
62 // BodyDef.position = b2Vec2(0, 0);
63 // BodyDef.type = b2_staticBody;
64 // m_groundBody = world->CreateBody(&BodyDef);
65 //
66 // b2EdgeShape edgeShape;
67 //
68 // b2FixtureDef firstR;
69 // edgeShape.Set( b2Vec2(0,m_posY), b2Vec2(m_runaway,m_posY) );
70 // firstR.shape = &edgeShape;
71 // m_groundBody->CreateFixture(&firstR);
72 //
73 // b2FixtureDef firstSlope;
74 // edgeShape.Set( b2Vec2(m_runaway,m_posY), b2Vec2(m_runaway+m_height*sin(m_angle),m_posY+m_height*cos(m_angle)) );
75 // firstSlope.shape = &edgeShape;
76 // m_groundBody->CreateFixture(&firstSlope);
77 //
78 // b2FixtureDef secondSlope;
79 // edgeShape.Set( b2Vec2(m_runaway+m_height*sin(m_angle),m_posY+m_height*cos(m_angle)), b2Vec2(m_runaway+2*m_height*sin(m_angle),m_posY) );
80 // secondSlope.shape = &edgeShape;
81 // m_groundBody->CreateFixture(&secondSlope);
82 //
83 // b2FixtureDef endR;
84 // edgeShape.Set( b2Vec2(m_runaway+2*m_height*sin(m_angle),m_posY), b2Vec2(4*m_runaway+2*m_height*sin(m_angle),m_posY) );
85 // endR.shape = &edgeShape;
86 // m_groundBody->CreateFixture(&endR);
87 }
88 
89 void Vterrain::drawBody(sf::RenderWindow& window){
90  sf::VertexArray lines(sf::LinesStrip, 5);
91  lines[0].position = sf::Vector2f(0, m_posY*m_M_TO_PX);
92  lines[1].position = sf::Vector2f(m_runaway*m_M_TO_PX, m_posY*m_M_TO_PX);
93  lines[2].position = sf::Vector2f((m_runaway+m_width/2)*m_M_TO_PX, (m_posY+m_height)*m_M_TO_PX);
94  lines[3].position = sf::Vector2f((m_runaway+m_width)*m_M_TO_PX, m_posY*m_M_TO_PX);
95  lines[4].position = sf::Vector2f((2*m_runaway+m_width)*m_M_TO_PX, m_posY*m_M_TO_PX);
96 
97 // lines[0].position = sf::Vector2f(0, m_posY*m_M_TO_PX);
98 // lines[1].position = sf::Vector2f(m_runaway*m_M_TO_PX, m_posY*m_M_TO_PX);
99 // lines[2].position = sf::Vector2f((m_runaway+m_height*sin(m_angle))*m_M_TO_PX, (m_posY+m_height*cos(m_angle))*m_M_TO_PX);
100 // lines[3].position = sf::Vector2f((m_runaway+2*m_height*sin(m_angle))*m_M_TO_PX, m_posY*m_M_TO_PX);
101 // lines[4].position = sf::Vector2f((2*m_runaway+2*m_height*sin(m_angle))*m_M_TO_PX, m_posY*m_M_TO_PX);
102 
103  lines[0].color = sf::Color::Black;
104  lines[1].color = sf::Color::Black;
105  lines[2].color = sf::Color::Black;
106  lines[3].color = sf::Color::Black;
107  lines[4].color = sf::Color::Black;
108 
109  window.draw(lines);
110 }
111 
114  return b2Vec2(m_runaway, m_posY);
115 }
118  return b2Vec2((m_runaway+m_width), m_posY);
119 }
122  return b2Vec2((m_runaway+m_width/2), (m_posY+m_height));
123 }
124 
125 double Vterrain::getVLength(){
126  double l = distance(m_runaway, m_posY, m_runaway+m_width/2, m_posY+m_height);
127  l += distance(m_runaway+m_width, m_posY, m_runaway+m_width/2, m_posY+m_height);
128  return l;
129 }
130 
132  return V_TERRAIN;
133 }
134 
void create(b2World *world, sf::RenderWindow &window, config::sTerrain terrainParam, int WINDOW_X_PX, double bodyLength=1)
Definition: Vterrain.cpp:25
void createBody(b2World *world)
Definition: Vterrain.cpp:31
b2Vec2 getBottom()
Definition: Vterrain.cpp:121
void drawBody(sf::RenderWindow &window)
Definition: Vterrain.cpp:89
b2Vec2 getTopRightCorner()
Definition: Vterrain.cpp:117
e_terrain_type
Definition: Terrain.h:23
virtual void create(b2World *world, sf::RenderWindow &window, config::sTerrain terrainParam, int WINDOW_X_PX, double bodyLength)
Definition: Terrain.cpp:38
Implementation of the Vterrain class which inherit from the Terrain class.
e_terrain_type getType()
Definition: Vterrain.cpp:131
b2Vec2 getTopLeftCorner()
Definition: Vterrain.cpp:113