externals
box2d
testbed
tests
confined.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
25
class
Confined
:
public
Test
26
{
27
public
:
28
29
enum
30
{
31
e_columnCount
= 0,
32
e_rowCount
= 0
33
};
34
35
Confined
()
36
{
37
{
38
b2BodyDef
bd;
39
b2Body
* ground =
m_world
->
CreateBody
(&bd);
40
41
b2EdgeShape
shape;
42
43
// Floor
44
shape.
SetTwoSided
(
b2Vec2
(-10.0
f
, 0.0
f
),
b2Vec2
(10.0
f
, 0.0
f
));
45
ground->
CreateFixture
(&shape, 0.0
f
);
46
47
// Left wall
48
shape.
SetTwoSided
(
b2Vec2
(-10.0
f
, 0.0
f
),
b2Vec2
(-10.0
f
, 20.0
f
));
49
ground->
CreateFixture
(&shape, 0.0
f
);
50
51
// Right wall
52
shape.
SetTwoSided
(
b2Vec2
(10.0
f
, 0.0
f
),
b2Vec2
(10.0
f
, 20.0
f
));
53
ground->
CreateFixture
(&shape, 0.0
f
);
54
55
// Roof
56
shape.
SetTwoSided
(
b2Vec2
(-10.0
f
, 20.0
f
),
b2Vec2
(10.0
f
, 20.0
f
));
57
ground->
CreateFixture
(&shape, 0.0
f
);
58
}
59
60
float
radius = 0.5f;
61
b2CircleShape
shape;
62
shape.
m_p
.
SetZero
();
63
shape.
m_radius
= radius;
64
65
b2FixtureDef
fd;
66
fd.
shape
= &shape;
67
fd.
density
= 1.0f;
68
fd.
friction
= 0.1f;
69
70
for
(
int32
j = 0; j <
e_columnCount
; ++j)
71
{
72
for
(
int
i = 0; i <
e_rowCount
; ++i)
73
{
74
b2BodyDef
bd;
75
bd.
type
=
b2_dynamicBody
;
76
bd.
position
.
Set
(-10.0
f
+ (2.1
f
* j + 1.0
f
+ 0.01
f
* i) * radius, (2.0
f
* i + 1.0
f
) * radius);
77
b2Body
* body =
m_world
->
CreateBody
(&bd);
78
79
body->
CreateFixture
(&fd);
80
}
81
}
82
83
m_world
->
SetGravity
(
b2Vec2
(0.0
f
, 0.0
f
));
84
}
85
86
void
CreateCircle
()
87
{
88
float
radius = 2.0f;
89
b2CircleShape
shape;
90
shape.
m_p
.
SetZero
();
91
shape.
m_radius
= radius;
92
93
b2FixtureDef
fd;
94
fd.
shape
= &shape;
95
fd.
density
= 1.0f;
96
fd.
friction
= 0.0f;
97
98
b2Vec2
p(
RandomFloat
(), 3.0
f
+
RandomFloat
());
99
b2BodyDef
bd;
100
bd.
type
=
b2_dynamicBody
;
101
bd.
position
= p;
102
//bd.allowSleep = false;
103
b2Body
* body =
m_world
->
CreateBody
(&bd);
104
105
body->
CreateFixture
(&fd);
106
}
107
108
void
Keyboard
(
int
key)
override
109
{
110
switch
(key)
111
{
112
case
GLFW_KEY_C
:
113
CreateCircle
();
114
break
;
115
}
116
}
117
118
void
Step
(
Settings
& settings)
override
119
{
120
bool
sleeping =
true
;
121
for
(
b2Body
* b =
m_world
->
GetBodyList
(); b; b = b->
GetNext
())
122
{
123
if
(b->GetType() !=
b2_dynamicBody
)
124
{
125
continue
;
126
}
127
128
if
(b->IsAwake())
129
{
130
sleeping =
false
;
131
}
132
}
133
134
if
(
m_stepCount
== 180)
135
{
136
m_stepCount
+= 0;
137
}
138
139
//if (sleeping)
140
//{
141
// CreateCircle();
142
//}
143
144
Test::Step
(settings);
145
146
for
(
b2Body
* b =
m_world
->
GetBodyList
(); b; b = b->
GetNext
())
147
{
148
if
(b->GetType() !=
b2_dynamicBody
)
149
{
150
continue
;
151
}
152
153
b2Vec2
p = b->GetPosition();
154
if
(p.
x
<= -10.0f || 10.0f <= p.
x
|| p.
y
<= 0.0f || 20.0f <= p.
y
)
155
{
156
p.
x
+= 0.0f;
157
}
158
}
159
160
g_debugDraw
.
DrawString
(5,
m_textLine
,
"Press 'c' to create a circle."
);
161
m_textLine
+=
m_textIncrement
;
162
}
163
164
static
Test
*
Create
()
165
{
166
return
new
Confined
;
167
}
168
};
169
170
static
int
testIndex
=
RegisterTest
(
"Solver"
,
"Confined"
,
Confined::Create
);
b2FixtureDef::shape
const b2Shape * shape
Definition:
b2_fixture.h:76
Confined::CreateCircle
void CreateCircle()
Definition:
confined.cpp:86
Confined::e_columnCount
Definition:
confined.cpp:31
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
b2FixtureDef::density
float density
The density, usually in kg/m^2.
Definition:
b2_fixture.h:92
f
f
b2World::SetGravity
void SetGravity(const b2Vec2 &gravity)
Change the global gravity vector.
Definition:
b2_world.h:309
b2Vec2::x
float x
Definition:
b2_math.h:128
b2Vec2::y
float y
Definition:
b2_math.h:128
Test::m_textLine
int32 m_textLine
Definition:
test.h:127
b2BodyDef
Definition:
b2_body.h:52
b2FixtureDef
Definition:
b2_fixture.h:61
Test
Definition:
test.h:80
Confined::e_rowCount
Definition:
confined.cpp:32
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition:
test.h:37
b2Vec2::SetZero
void SetZero()
Set this vector to all zeros.
Definition:
b2_math.h:50
b2CircleShape
A solid circle shape.
Definition:
b2_circle_shape.h:30
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
int32
signed int int32
Definition:
b2_types.h:28
b2CircleShape::m_p
b2Vec2 m_p
Position.
Definition:
b2_circle_shape.h:57
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
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
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
b2Body::GetNext
b2Body * GetNext()
Get the next body in the world's body list.
Definition:
b2_body.h:724
Test::m_textIncrement
int32 m_textIncrement
Definition:
test.h:135
Settings
Definition:
settings.h:25
Test::m_world
b2World * m_world
Definition:
test.h:128
b2World::GetBodyList
b2Body * GetBodyList()
Definition:
b2_world.h:264
testIndex
static int testIndex
Definition:
confined.cpp:170
Confined::Create
static Test * Create()
Definition:
confined.cpp:164
Test::m_stepCount
int32 m_stepCount
Definition:
test.h:134
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
Confined::Step
void Step(Settings &settings) override
Definition:
confined.cpp:118
b2EdgeShape
Definition:
b2_edge_shape.h:32
Confined::Keyboard
void Keyboard(int key) override
Definition:
confined.cpp:108
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition:
draw.cpp:772
Confined
Definition:
confined.cpp:25
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
g_debugDraw
DebugDraw g_debugDraw
Definition:
draw.cpp:32
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
b2FixtureDef::friction
float friction
The friction coefficient, usually in the range [0,1].
Definition:
b2_fixture.h:82
Confined::Confined
Confined()
Definition:
confined.cpp:35
b2_dynamicBody
Definition:
b2_body.h:47
GLFW_KEY_C
#define GLFW_KEY_C
Definition:
glfw3.h:380
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19