edge_test.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "test.h"
24 #include "imgui/imgui.h"
25 
26 class EdgeTest : public Test
27 {
28 public:
29 
31  {
32  b2Vec2 vertices[10] =
33  {
34  {10.0f, -4.0f},
35  {10.0f, 0.0f},
36  {6.0f, 0.0f},
37  {4.0f, 2.0f},
38  {2.0f, 0.0f},
39  {-2.0f, 0.0f},
40  {-6.0f, 0.0f},
41  {-8.0f, -3.0f},
42  {-10.0f, 0.0f},
43  {-10.0f, -4.0f}
44  };
45 
46  m_offset1.Set(0.0f, 8.0f);
47  m_offset2.Set(0.0f, 16.0f);
48 
49  {
50  b2Vec2 v1 = vertices[0] + m_offset1;
51  b2Vec2 v2 = vertices[1] + m_offset1;
52  b2Vec2 v3 = vertices[2] + m_offset1;
53  b2Vec2 v4 = vertices[3] + m_offset1;
54  b2Vec2 v5 = vertices[4] + m_offset1;
55  b2Vec2 v6 = vertices[5] + m_offset1;
56  b2Vec2 v7 = vertices[6] + m_offset1;
57  b2Vec2 v8 = vertices[7] + m_offset1;
58  b2Vec2 v9 = vertices[8] + m_offset1;
59  b2Vec2 v10 = vertices[9] + m_offset1;
60 
61  b2BodyDef bd;
62  b2Body* ground = m_world->CreateBody(&bd);
63 
64  b2EdgeShape shape;
65 
66  shape.SetOneSided(v10, v1, v2, v3);
67  ground->CreateFixture(&shape, 0.0f);
68 
69  shape.SetOneSided(v1, v2, v3, v4);
70  ground->CreateFixture(&shape, 0.0f);
71 
72  shape.SetOneSided(v2, v3, v4, v5);
73  ground->CreateFixture(&shape, 0.0f);
74 
75  shape.SetOneSided(v3, v4, v5, v6);
76  ground->CreateFixture(&shape, 0.0f);
77 
78  shape.SetOneSided(v4, v5, v6, v7);
79  ground->CreateFixture(&shape, 0.0f);
80 
81  shape.SetOneSided(v5, v6, v7, v8);
82  ground->CreateFixture(&shape, 0.0f);
83 
84  shape.SetOneSided(v6, v7, v8, v9);
85  ground->CreateFixture(&shape, 0.0f);
86 
87  shape.SetOneSided(v7, v8, v9, v10);
88  ground->CreateFixture(&shape, 0.0f);
89 
90  shape.SetOneSided(v8, v9, v10, v1);
91  ground->CreateFixture(&shape, 0.0f);
92 
93  shape.SetOneSided(v9, v10, v1, v2);
94  ground->CreateFixture(&shape, 0.0f);
95  }
96 
97  {
98  b2Vec2 v1 = vertices[0] + m_offset2;
99  b2Vec2 v2 = vertices[1] + m_offset2;
100  b2Vec2 v3 = vertices[2] + m_offset2;
101  b2Vec2 v4 = vertices[3] + m_offset2;
102  b2Vec2 v5 = vertices[4] + m_offset2;
103  b2Vec2 v6 = vertices[5] + m_offset2;
104  b2Vec2 v7 = vertices[6] + m_offset2;
105  b2Vec2 v8 = vertices[7] + m_offset2;
106  b2Vec2 v9 = vertices[8] + m_offset2;
107  b2Vec2 v10 = vertices[9] + m_offset2;
108 
109  b2BodyDef bd;
110  b2Body* ground = m_world->CreateBody(&bd);
111 
112  b2EdgeShape shape;
113 
114  shape.SetTwoSided(v1, v2);
115  ground->CreateFixture(&shape, 0.0f);
116 
117  shape.SetTwoSided(v2, v3);
118  ground->CreateFixture(&shape, 0.0f);
119 
120  shape.SetTwoSided(v3, v4);
121  ground->CreateFixture(&shape, 0.0f);
122 
123  shape.SetTwoSided(v4, v5);
124  ground->CreateFixture(&shape, 0.0f);
125 
126  shape.SetTwoSided(v5, v6);
127  ground->CreateFixture(&shape, 0.0f);
128 
129  shape.SetTwoSided(v6, v7);
130  ground->CreateFixture(&shape, 0.0f);
131 
132  shape.SetTwoSided(v7, v8);
133  ground->CreateFixture(&shape, 0.0f);
134 
135  shape.SetTwoSided(v8, v9);
136  ground->CreateFixture(&shape, 0.0f);
137 
138  shape.SetTwoSided(v9, v10);
139  ground->CreateFixture(&shape, 0.0f);
140 
141  shape.SetTwoSided(v10, v1);
142  ground->CreateFixture(&shape, 0.0f);
143  }
144 
145  m_body1 = nullptr;
146  m_body2 = nullptr;
147  CreateBoxes();
148  m_boxes = true;
149  }
150 
151  void CreateBoxes()
152  {
153  if (m_body1)
154  {
156  m_body1 = nullptr;
157  }
158 
159  if (m_body2)
160  {
162  m_body2 = nullptr;
163  }
164 
165  {
166  b2BodyDef bd;
167  bd.type = b2_dynamicBody;
168  bd.position = b2Vec2(8.0f, 2.6f) + m_offset1;
169  bd.allowSleep = false;
170  m_body1 = m_world->CreateBody(&bd);
171 
172  b2PolygonShape shape;
173  shape.SetAsBox(0.5f, 1.0f);
174 
175  m_body1->CreateFixture(&shape, 1.0f);
176  }
177 
178  {
179  b2BodyDef bd;
180  bd.type = b2_dynamicBody;
181  bd.position = b2Vec2(8.0f, 2.6f) + m_offset2;
182  bd.allowSleep = false;
183  m_body2 = m_world->CreateBody(&bd);
184 
185  b2PolygonShape shape;
186  shape.SetAsBox(0.5f, 1.0f);
187 
188  m_body2->CreateFixture(&shape, 1.0f);
189  }
190  }
191 
193  {
194  if (m_body1)
195  {
197  m_body1 = nullptr;
198  }
199 
200  if (m_body2)
201  {
203  m_body2 = nullptr;
204  }
205 
206  {
207  b2BodyDef bd;
208  bd.type = b2_dynamicBody;
209  bd.position = b2Vec2(-0.5f, 0.6f) + m_offset1;
210  bd.allowSleep = false;
211  m_body1 = m_world->CreateBody(&bd);
212 
213  b2CircleShape shape;
214  shape.m_radius = 0.5f;
215 
216  m_body1->CreateFixture(&shape, 1.0f);
217  }
218 
219  {
220  b2BodyDef bd;
221  bd.type = b2_dynamicBody;
222  bd.position = b2Vec2(-0.5f, 0.6f) + m_offset2;
223  bd.allowSleep = false;
224  m_body2 = m_world->CreateBody(&bd);
225 
226  b2CircleShape shape;
227  shape.m_radius = 0.5f;
228 
229  m_body2->CreateFixture(&shape, 1.0f);
230  }
231  }
232 
233  void UpdateUI() override
234  {
235  ImGui::SetNextWindowPos(ImVec2(10.0f, 100.0f));
236  ImGui::SetNextWindowSize(ImVec2(200.0f, 100.0f));
237  ImGui::Begin("Custom Controls", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
238 
239  if (ImGui::RadioButton("Boxes", m_boxes == true))
240  {
241  CreateBoxes();
242  m_boxes = true;
243  }
244 
245  if (ImGui::RadioButton("Circles", m_boxes == false))
246  {
247  CreateCircles();
248  m_boxes = false;
249  }
250 
251  ImGui::End();
252  }
253 
254  void Step(Settings& settings) override
255  {
257  {
258  m_body1->ApplyForceToCenter(b2Vec2(-10.0f, 0.0f), true);
259  m_body2->ApplyForceToCenter(b2Vec2(-10.0f, 0.0f), true);
260  }
261 
263  {
264  m_body1->ApplyForceToCenter(b2Vec2(10.0f, 0.0f), true);
265  m_body2->ApplyForceToCenter(b2Vec2(10.0f, 0.0f), true);
266  }
267 
268  Test::Step(settings);
269  }
270 
271  static Test* Create()
272  {
273  return new EdgeTest;
274  }
275 
279  bool m_boxes;
280 };
281 
282 static int testIndex = RegisterTest("Geometry", "Edge Test", EdgeTest::Create);
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition: imgui.cpp:6054
IMGUI_API bool RadioButton(const char *label, bool active)
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
static int testIndex
Definition: edge_test.cpp:282
f
bool m_boxes
Definition: edge_test.cpp:279
void ApplyForceToCenter(const b2Vec2 &force, bool wake)
Definition: b2_body.h:759
Definition: imgui.h:164
static Test * Create()
Definition: edge_test.cpp:271
Definition: test.h:80
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
#define GLFW_KEY_A
Definition: glfw3.h:378
void CreateCircles()
Definition: edge_test.cpp:192
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
void SetOneSided(const b2Vec2 &v0, const b2Vec2 &v1, const b2Vec2 &v2, const b2Vec2 &v3)
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition: imgui.cpp:4736
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
float m_radius
Definition: b2_shape.h:102
b2Body * m_body2
Definition: edge_test.cpp:278
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
IMGUI_API void End()
Definition: imgui.cpp:5371
#define GLFW_KEY_D
Definition: glfw3.h:381
b2World * m_world
Definition: test.h:128
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition: imgui.cpp:6045
b2Vec2 position
Definition: b2_body.h:78
void DestroyBody(b2Body *body)
Definition: b2_world.cpp:139
b2Vec2 m_offset2
Definition: edge_test.cpp:276
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void UpdateUI() override
Definition: edge_test.cpp:233
bool allowSleep
Definition: b2_body.h:103
GLFWwindow * g_mainWindow
Definition: main.cpp:42
virtual void Step(Settings &settings)
Definition: test.cpp:278
#define GLFW_PRESS
The key or mouse button was pressed.
Definition: glfw3.h:304
void Step(Settings &settings) override
Definition: edge_test.cpp:254
b2Vec2 m_offset1
Definition: edge_test.cpp:276
GLFWAPI int glfwGetKey(GLFWwindow *window, int key)
Returns the last reported state of a keyboard key for the specified window.
Definition: input.c:591
void CreateBoxes()
Definition: edge_test.cpp:151
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115
b2Body * m_body1
Definition: edge_test.cpp:277


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:20