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