externals
box2d
testbed
tests
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
30
EdgeTest
()
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.0
f
, 8.0
f
);
47
m_offset2
.
Set
(0.0
f
, 16.0
f
);
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.0
f
);
68
69
shape.
SetOneSided
(v1, v2, v3, v4);
70
ground->
CreateFixture
(&shape, 0.0
f
);
71
72
shape.
SetOneSided
(v2, v3, v4, v5);
73
ground->
CreateFixture
(&shape, 0.0
f
);
74
75
shape.
SetOneSided
(v3, v4, v5, v6);
76
ground->
CreateFixture
(&shape, 0.0
f
);
77
78
shape.
SetOneSided
(v4, v5, v6, v7);
79
ground->
CreateFixture
(&shape, 0.0
f
);
80
81
shape.
SetOneSided
(v5, v6, v7, v8);
82
ground->
CreateFixture
(&shape, 0.0
f
);
83
84
shape.
SetOneSided
(v6, v7, v8, v9);
85
ground->
CreateFixture
(&shape, 0.0
f
);
86
87
shape.
SetOneSided
(v7, v8, v9, v10);
88
ground->
CreateFixture
(&shape, 0.0
f
);
89
90
shape.
SetOneSided
(v8, v9, v10, v1);
91
ground->
CreateFixture
(&shape, 0.0
f
);
92
93
shape.
SetOneSided
(v9, v10, v1, v2);
94
ground->
CreateFixture
(&shape, 0.0
f
);
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.0
f
);
116
117
shape.
SetTwoSided
(v2, v3);
118
ground->
CreateFixture
(&shape, 0.0
f
);
119
120
shape.
SetTwoSided
(v3, v4);
121
ground->
CreateFixture
(&shape, 0.0
f
);
122
123
shape.
SetTwoSided
(v4, v5);
124
ground->
CreateFixture
(&shape, 0.0
f
);
125
126
shape.
SetTwoSided
(v5, v6);
127
ground->
CreateFixture
(&shape, 0.0
f
);
128
129
shape.
SetTwoSided
(v6, v7);
130
ground->
CreateFixture
(&shape, 0.0
f
);
131
132
shape.
SetTwoSided
(v7, v8);
133
ground->
CreateFixture
(&shape, 0.0
f
);
134
135
shape.
SetTwoSided
(v8, v9);
136
ground->
CreateFixture
(&shape, 0.0
f
);
137
138
shape.
SetTwoSided
(v9, v10);
139
ground->
CreateFixture
(&shape, 0.0
f
);
140
141
shape.
SetTwoSided
(v10, v1);
142
ground->
CreateFixture
(&shape, 0.0
f
);
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
{
155
m_world
->
DestroyBody
(
m_body1
);
156
m_body1
=
nullptr
;
157
}
158
159
if
(
m_body2
)
160
{
161
m_world
->
DestroyBody
(
m_body2
);
162
m_body2
=
nullptr
;
163
}
164
165
{
166
b2BodyDef
bd;
167
bd.
type
=
b2_dynamicBody
;
168
bd.
position
=
b2Vec2
(8.0
f
, 2.6
f
) +
m_offset1
;
169
bd.
allowSleep
=
false
;
170
m_body1
=
m_world
->
CreateBody
(&bd);
171
172
b2PolygonShape
shape;
173
shape.
SetAsBox
(0.5
f
, 1.0
f
);
174
175
m_body1
->
CreateFixture
(&shape, 1.0
f
);
176
}
177
178
{
179
b2BodyDef
bd;
180
bd.
type
=
b2_dynamicBody
;
181
bd.
position
=
b2Vec2
(8.0
f
, 2.6
f
) +
m_offset2
;
182
bd.
allowSleep
=
false
;
183
m_body2
=
m_world
->
CreateBody
(&bd);
184
185
b2PolygonShape
shape;
186
shape.
SetAsBox
(0.5
f
, 1.0
f
);
187
188
m_body2
->
CreateFixture
(&shape, 1.0
f
);
189
}
190
}
191
192
void
CreateCircles
()
193
{
194
if
(
m_body1
)
195
{
196
m_world
->
DestroyBody
(
m_body1
);
197
m_body1
=
nullptr
;
198
}
199
200
if
(
m_body2
)
201
{
202
m_world
->
DestroyBody
(
m_body2
);
203
m_body2
=
nullptr
;
204
}
205
206
{
207
b2BodyDef
bd;
208
bd.
type
=
b2_dynamicBody
;
209
bd.
position
=
b2Vec2
(-0.5
f
, 0.6
f
) +
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.0
f
);
217
}
218
219
{
220
b2BodyDef
bd;
221
bd.
type
=
b2_dynamicBody
;
222
bd.
position
=
b2Vec2
(-0.5
f
, 0.6
f
) +
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.0
f
);
230
}
231
}
232
233
void
UpdateUI
()
override
234
{
235
ImGui::SetNextWindowPos
(
ImVec2
(10.0
f
, 100.0
f
));
236
ImGui::SetNextWindowSize
(
ImVec2
(200.0
f
, 100.0
f
));
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
{
256
if
(
glfwGetKey
(
g_mainWindow
,
GLFW_KEY_A
) ==
GLFW_PRESS
)
257
{
258
m_body1
->
ApplyForceToCenter
(
b2Vec2
(-10.0
f
, 0.0
f
),
true
);
259
m_body2
->
ApplyForceToCenter
(
b2Vec2
(-10.0
f
, 0.0
f
),
true
);
260
}
261
262
if
(
glfwGetKey
(
g_mainWindow
,
GLFW_KEY_D
) ==
GLFW_PRESS
)
263
{
264
m_body1
->
ApplyForceToCenter
(
b2Vec2
(10.0
f
, 0.0
f
),
true
);
265
m_body2
->
ApplyForceToCenter
(
b2Vec2
(10.0
f
, 0.0
f
),
true
);
266
}
267
268
Test::Step
(settings);
269
}
270
271
static
Test
*
Create
()
272
{
273
return
new
EdgeTest
;
274
}
275
276
b2Vec2
m_offset1
,
m_offset2
;
277
b2Body
*
m_body1
;
278
b2Body
*
m_body2
;
279
bool
m_boxes
;
280
};
281
282
static
int
testIndex
=
RegisterTest
(
"Geometry"
,
"Edge Test"
,
EdgeTest::Create
);
ImGui::SetNextWindowSize
IMGUI_API void SetNextWindowSize(const ImVec2 &size, ImGuiCond cond=0)
Definition:
imgui.cpp:6054
ImGui::RadioButton
IMGUI_API bool RadioButton(const char *label, bool active)
Definition:
imgui_widgets.cpp:944
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
testIndex
static int testIndex
Definition:
edge_test.cpp:282
f
f
EdgeTest::m_boxes
bool m_boxes
Definition:
edge_test.cpp:279
b2Body::ApplyForceToCenter
void ApplyForceToCenter(const b2Vec2 &force, bool wake)
Definition:
b2_body.h:759
ImVec2
Definition:
imgui.h:164
b2BodyDef
Definition:
b2_body.h:52
EdgeTest::Create
static Test * Create()
Definition:
edge_test.cpp:271
Test
Definition:
test.h:80
imgui.h
EdgeTest::EdgeTest
EdgeTest()
Definition:
edge_test.cpp:30
b2CircleShape
A solid circle shape.
Definition:
b2_circle_shape.h:30
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
GLFW_KEY_A
#define GLFW_KEY_A
Definition:
glfw3.h:378
b2PolygonShape
Definition:
b2_polygon_shape.h:32
EdgeTest::CreateCircles
void CreateCircles()
Definition:
edge_test.cpp:192
b2EdgeShape::SetTwoSided
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
Definition:
b2_edge_shape.cpp:36
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition:
b2_polygon_shape.cpp:36
b2EdgeShape::SetOneSided
void SetOneSided(const b2Vec2 &v0, const b2Vec2 &v1, const b2Vec2 &v2, const b2Vec2 &v3)
Definition:
b2_edge_shape.cpp:27
ImGui::Begin
IMGUI_API bool Begin(const char *name, bool *p_open=NULL, ImGuiWindowFlags flags=0)
Definition:
imgui.cpp:4736
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
b2Shape::m_radius
float m_radius
Definition:
b2_shape.h:102
EdgeTest::m_body2
b2Body * m_body2
Definition:
edge_test.cpp:278
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
ImGui::End
IMGUI_API void End()
Definition:
imgui.cpp:5371
Settings
Definition:
settings.h:25
GLFW_KEY_D
#define GLFW_KEY_D
Definition:
glfw3.h:381
Test::m_world
b2World * m_world
Definition:
test.h:128
ImGuiWindowFlags_NoResize
Definition:
imgui.h:687
ImGui::SetNextWindowPos
IMGUI_API void SetNextWindowPos(const ImVec2 &pos, ImGuiCond cond=0, const ImVec2 &pivot=ImVec2(0, 0))
Definition:
imgui.cpp:6045
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
b2EdgeShape
Definition:
b2_edge_shape.h:32
b2World::DestroyBody
void DestroyBody(b2Body *body)
Definition:
b2_world.cpp:139
EdgeTest::m_offset2
b2Vec2 m_offset2
Definition:
edge_test.cpp:276
EdgeTest
Definition:
edge_test.cpp:26
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
EdgeTest::UpdateUI
void UpdateUI() override
Definition:
edge_test.cpp:233
b2BodyDef::allowSleep
bool allowSleep
Definition:
b2_body.h:103
g_mainWindow
GLFWwindow * g_mainWindow
Definition:
main.cpp:42
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
GLFW_PRESS
#define GLFW_PRESS
The key or mouse button was pressed.
Definition:
glfw3.h:304
EdgeTest::Step
void Step(Settings &settings) override
Definition:
edge_test.cpp:254
EdgeTest::m_offset1
b2Vec2 m_offset1
Definition:
edge_test.cpp:276
glfwGetKey
GLFWAPI int glfwGetKey(GLFWwindow *window, int key)
Returns the last reported state of a keyboard key for the specified window.
Definition:
input.c:591
EdgeTest::CreateBoxes
void CreateBoxes()
Definition:
edge_test.cpp:151
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
EdgeTest::m_body1
b2Body * m_body1
Definition:
edge_test.cpp:277
b2_dynamicBody
Definition:
b2_body.h:47
ImGuiWindowFlags_NoMove
Definition:
imgui.h:688
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:20