externals
box2d
testbed
tests
tiles.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
27
class
Tiles
:
public
Test
28
{
29
public
:
30
enum
31
{
32
e_count
= 20
33
};
34
35
Tiles
()
36
{
37
m_fixtureCount
= 0;
38
b2Timer
timer;
39
40
{
41
float
a = 0.5f;
42
b2BodyDef
bd;
43
bd.
position
.
y
= -a;
44
b2Body
* ground =
m_world
->
CreateBody
(&bd);
45
46
#if 1
47
int32
N = 200;
48
int32
M = 10;
49
b2Vec2
position;
50
position.
y
= 0.0f;
51
for
(
int32
j = 0; j < M; ++j)
52
{
53
position.
x
= -N * a;
54
for
(
int32
i = 0; i < N; ++i)
55
{
56
b2PolygonShape
shape;
57
shape.
SetAsBox
(a, a, position, 0.0
f
);
58
ground->
CreateFixture
(&shape, 0.0
f
);
59
++
m_fixtureCount
;
60
position.
x
+= 2.0f * a;
61
}
62
position.
y
-= 2.0f * a;
63
}
64
#else
65
int32
N = 200;
66
int32
M = 10;
67
b2Vec2
position;
68
position.
x
= -N * a;
69
for
(
int32
i = 0; i < N; ++i)
70
{
71
position.
y
= 0.0f;
72
for
(
int32
j = 0; j < M; ++j)
73
{
74
b2PolygonShape
shape;
75
shape.
SetAsBox
(a, a, position, 0.0
f
);
76
ground->
CreateFixture
(&shape, 0.0
f
);
77
position.
y
-= 2.0f * a;
78
}
79
position.
x
+= 2.0f * a;
80
}
81
#endif
82
}
83
84
{
85
float
a = 0.5f;
86
b2PolygonShape
shape;
87
shape.
SetAsBox
(a, a);
88
89
b2Vec2
x(-7.0
f
, 0.75
f
);
90
b2Vec2
y;
91
b2Vec2
deltaX(0.5625
f
, 1.25
f
);
92
b2Vec2
deltaY(1.125
f
, 0.0
f
);
93
94
for
(
int32
i = 0; i <
e_count
; ++i)
95
{
96
y = x;
97
98
for
(
int32
j = i; j <
e_count
; ++j)
99
{
100
b2BodyDef
bd;
101
bd.
type
=
b2_dynamicBody
;
102
bd.
position
= y;
103
104
//if (i == 0 && j == 0)
105
//{
106
// bd.allowSleep = false;
107
//}
108
//else
109
//{
110
// bd.allowSleep = true;
111
//}
112
113
b2Body
* body =
m_world
->
CreateBody
(&bd);
114
body->
CreateFixture
(&shape, 5.0
f
);
115
++
m_fixtureCount
;
116
y += deltaY;
117
}
118
119
x += deltaX;
120
}
121
}
122
123
m_createTime
= timer.
GetMilliseconds
();
124
}
125
126
void
Step
(
Settings
& settings)
override
127
{
128
const
b2ContactManager
& cm =
m_world
->
GetContactManager
();
129
int32
height = cm.
m_broadPhase
.
GetTreeHeight
();
130
int32
leafCount = cm.
m_broadPhase
.
GetProxyCount
();
131
int32
minimumNodeCount = 2 * leafCount - 1;
132
float
minimumHeight = ceilf(logf(
float
(minimumNodeCount)) / logf(2.0
f
));
133
g_debugDraw
.
DrawString
(5,
m_textLine
,
"dynamic tree height = %d, min = %d"
, height,
int32
(minimumHeight));
134
m_textLine
+=
m_textIncrement
;
135
136
Test::Step
(settings);
137
138
g_debugDraw
.
DrawString
(5,
m_textLine
,
"create time = %6.2f ms, fixture count = %d"
,
139
m_createTime
,
m_fixtureCount
);
140
m_textLine
+=
m_textIncrement
;
141
142
//b2DynamicTree* tree = &m_world->m_contactManager.m_broadPhase.m_tree;
143
144
//if (m_stepCount == 400)
145
//{
146
// tree->RebuildBottomUp();
147
//}
148
}
149
150
static
Test
*
Create
()
151
{
152
return
new
Tiles
;
153
}
154
155
int32
m_fixtureCount
;
156
float
m_createTime
;
157
};
158
159
static
int
testIndex
=
RegisterTest
(
"Benchmark"
,
"Tiles"
,
Tiles::Create
);
b2Timer
Definition:
b2_timer.h:31
testIndex
static int testIndex
Definition:
tiles.cpp:159
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
b2World::GetContactManager
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition:
b2_world.h:335
Tiles::m_createTime
float m_createTime
Definition:
tiles.cpp:156
f
f
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
Tiles::Tiles
Tiles()
Definition:
tiles.cpp:35
Test
Definition:
test.h:80
Tiles
Definition:
tiles.cpp:27
Tiles::e_count
Definition:
tiles.cpp:32
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
int32
signed int int32
Definition:
b2_types.h:28
b2PolygonShape
Definition:
b2_polygon_shape.h:32
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition:
b2_polygon_shape.cpp:36
b2BroadPhase::GetProxyCount
int32 GetProxyCount() const
Get the number of proxies.
Definition:
b2_broad_phase.h:152
Tiles::m_fixtureCount
int32 m_fixtureCount
Definition:
tiles.cpp:155
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
b2BroadPhase::GetTreeHeight
int32 GetTreeHeight() const
Get the height of the embedded tree.
Definition:
b2_broad_phase.h:157
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
Tiles::Step
void Step(Settings &settings) override
Definition:
tiles.cpp:126
b2ContactManager
Definition:
b2_contact_manager.h:35
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
b2ContactManager::m_broadPhase
b2BroadPhase m_broadPhase
Definition:
b2_contact_manager.h:49
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
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
Tiles::Create
static Test * Create()
Definition:
tiles.cpp:150
b2_dynamicBody
Definition:
b2_body.h:47
b2Timer::GetMilliseconds
float GetMilliseconds() const
Get the time since construction or the last reset.
Definition:
b2_timer.cpp:120
mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:21