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