externals
box2d
testbed
tests
continuous_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
25
class
ContinuousTest
:
public
Test
26
{
27
public
:
28
29
ContinuousTest
()
30
{
31
{
32
b2BodyDef
bd;
33
bd.
position
.
Set
(0.0
f
, 0.0
f
);
34
b2Body
* body =
m_world
->
CreateBody
(&bd);
35
36
b2EdgeShape
edge;
37
38
edge.
SetTwoSided
(
b2Vec2
(-10.0
f
, 0.0
f
),
b2Vec2
(10.0
f
, 0.0
f
));
39
body->
CreateFixture
(&edge, 0.0
f
);
40
41
b2PolygonShape
shape;
42
shape.
SetAsBox
(0.2
f
, 1.0
f
,
b2Vec2
(0.5
f
, 1.0
f
), 0.0
f
);
43
body->
CreateFixture
(&shape, 0.0
f
);
44
}
45
46
#if 1
47
{
48
b2BodyDef
bd;
49
bd.
type
=
b2_dynamicBody
;
50
bd.
position
.
Set
(0.0
f
, 20.0
f
);
51
//bd.angle = 0.1f;
52
53
b2PolygonShape
shape;
54
shape.
SetAsBox
(2.0
f
, 0.1
f
);
55
56
m_body
=
m_world
->
CreateBody
(&bd);
57
m_body
->
CreateFixture
(&shape, 1.0
f
);
58
59
m_angularVelocity
=
RandomFloat
(-50.0
f
, 50.0
f
);
60
//m_angularVelocity = 46.661274f;
61
m_body
->
SetLinearVelocity
(
b2Vec2
(0.0
f
, -100.0
f
));
62
m_body
->
SetAngularVelocity
(
m_angularVelocity
);
63
}
64
#else
65
{
66
b2BodyDef
bd;
67
bd.
type
=
b2_dynamicBody
;
68
bd.
position
.
Set
(0.0
f
, 2.0
f
);
69
b2Body
* body =
m_world
->
CreateBody
(&bd);
70
71
b2CircleShape
shape;
72
shape.
m_p
.
SetZero
();
73
shape.
m_radius
= 0.5f;
74
body->
CreateFixture
(&shape, 1.0
f
);
75
76
bd.
bullet
=
true
;
77
bd.
position
.
Set
(0.0
f
, 10.0
f
);
78
body =
m_world
->
CreateBody
(&bd);
79
body->
CreateFixture
(&shape, 1.0
f
);
80
body->
SetLinearVelocity
(
b2Vec2
(0.0
f
, -100.0
f
));
81
}
82
#endif
83
84
extern
B2_API
int32
b2_gjkCalls
,
b2_gjkIters
,
b2_gjkMaxIters
;
85
extern
B2_API
int32
b2_toiCalls
,
b2_toiIters
;
86
extern
B2_API
int32
b2_toiRootIters
,
b2_toiMaxRootIters
;
87
extern
B2_API
float
b2_toiTime
,
b2_toiMaxTime
;
88
89
b2_gjkCalls
= 0;
b2_gjkIters
= 0;
b2_gjkMaxIters
= 0;
90
b2_toiCalls
= 0;
b2_toiIters
= 0;
91
b2_toiRootIters
= 0;
b2_toiMaxRootIters
= 0;
92
b2_toiTime
= 0.0f;
b2_toiMaxTime
= 0.0f;
93
}
94
95
void
Launch
()
96
{
97
extern
B2_API
int32
b2_gjkCalls
,
b2_gjkIters
,
b2_gjkMaxIters
;
98
extern
B2_API
int32
b2_toiCalls
,
b2_toiIters
;
99
extern
B2_API
int32
b2_toiRootIters
,
b2_toiMaxRootIters
;
100
extern
B2_API
float
b2_toiTime
,
b2_toiMaxTime
;
101
102
b2_gjkCalls
= 0;
b2_gjkIters
= 0;
b2_gjkMaxIters
= 0;
103
b2_toiCalls
= 0;
b2_toiIters
= 0;
104
b2_toiRootIters
= 0;
b2_toiMaxRootIters
= 0;
105
b2_toiTime
= 0.0f;
b2_toiMaxTime
= 0.0f;
106
107
m_body
->
SetTransform
(
b2Vec2
(0.0
f
, 20.0
f
), 0.0
f
);
108
m_angularVelocity
=
RandomFloat
(-50.0
f
, 50.0
f
);
109
m_body
->
SetLinearVelocity
(
b2Vec2
(0.0
f
, -100.0
f
));
110
m_body
->
SetAngularVelocity
(
m_angularVelocity
);
111
}
112
113
void
Step
(
Settings
& settings)
override
114
{
115
Test::Step
(settings);
116
117
extern
B2_API
int32
b2_gjkCalls
,
b2_gjkIters
,
b2_gjkMaxIters
;
118
119
if
(
b2_gjkCalls
> 0)
120
{
121
g_debugDraw
.
DrawString
(5,
m_textLine
,
"gjk calls = %d, ave gjk iters = %3.1f, max gjk iters = %d"
,
122
b2_gjkCalls
,
b2_gjkIters
/
float
(
b2_gjkCalls
),
b2_gjkMaxIters
);
123
m_textLine
+=
m_textIncrement
;
124
}
125
126
extern
B2_API
int32
b2_toiCalls
,
b2_toiIters
;
127
extern
B2_API
int32
b2_toiRootIters
,
b2_toiMaxRootIters
;
128
extern
B2_API
float
b2_toiTime
,
b2_toiMaxTime
;
129
130
if
(
b2_toiCalls
> 0)
131
{
132
g_debugDraw
.
DrawString
(5,
m_textLine
,
"toi calls = %d, ave [max] toi iters = %3.1f [%d]"
,
133
b2_toiCalls
,
b2_toiIters
/
float
(
b2_toiCalls
),
b2_toiMaxRootIters
);
134
m_textLine
+=
m_textIncrement
;
135
136
g_debugDraw
.
DrawString
(5,
m_textLine
,
"ave [max] toi root iters = %3.1f [%d]"
,
137
b2_toiRootIters
/
float
(
b2_toiCalls
),
b2_toiMaxRootIters
);
138
m_textLine
+=
m_textIncrement
;
139
140
g_debugDraw
.
DrawString
(5,
m_textLine
,
"ave [max] toi time = %.1f [%.1f] (microseconds)"
,
141
1000.0
f
*
b2_toiTime
/
float
(
b2_toiCalls
), 1000.0
f
*
b2_toiMaxTime
);
142
m_textLine
+=
m_textIncrement
;
143
}
144
145
if
(
m_stepCount
% 60 == 0)
146
{
147
//Launch();
148
}
149
}
150
151
static
Test
*
Create
()
152
{
153
return
new
ContinuousTest
;
154
}
155
156
b2Body
*
m_body
;
157
float
m_angularVelocity
;
158
};
159
160
static
int
testIndex
=
RegisterTest
(
"Continuous"
,
"Continuous Test"
,
ContinuousTest::Create
);
Test::m_textIncrement
int32 m_textIncrement
Definition:
test.h:135
b2_gjkIters
B2_API int32 b2_gjkIters
Definition:
b2_distance.cpp:30
b2EdgeShape
Definition:
b2_edge_shape.h:32
b2BodyDef::bullet
bool bullet
Definition:
b2_body.h:115
g_debugDraw
DebugDraw g_debugDraw
Definition:
draw.cpp:32
b2_toiCalls
B2_API int32 b2_toiCalls
Definition:
b2_time_of_impact.cpp:33
b2Body::SetTransform
void SetTransform(const b2Vec2 &position, float angle)
Definition:
b2_body.cpp:415
Test::m_stepCount
int32 m_stepCount
Definition:
test.h:134
ContinuousTest::Create
static Test * Create()
Definition:
continuous_test.cpp:151
ContinuousTest::ContinuousTest
ContinuousTest()
Definition:
continuous_test.cpp:29
b2Body
A rigid body. These are created via b2World::CreateBody.
Definition:
b2_body.h:128
b2CircleShape
A solid circle shape.
Definition:
b2_circle_shape.h:30
b2Vec2::SetZero
void SetZero()
Set this vector to all zeros.
Definition:
b2_math.h:50
B2_API
#define B2_API
Definition:
b2_api.h:49
b2Vec2::Set
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition:
b2_math.h:53
b2_toiRootIters
B2_API int32 b2_toiRootIters
Definition:
b2_time_of_impact.cpp:34
b2Vec2
A 2D column vector.
Definition:
b2_math.h:41
b2Body::SetAngularVelocity
void SetAngularVelocity(float omega)
Definition:
b2_body.h:524
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
b2Body::SetLinearVelocity
void SetLinearVelocity(const b2Vec2 &v)
Definition:
b2_body.h:504
testIndex
static int testIndex
Definition:
continuous_test.cpp:160
f
f
b2BodyDef::type
b2BodyType type
Definition:
b2_body.h:74
ContinuousTest::m_body
b2Body * m_body
Definition:
continuous_test.cpp:156
b2_toiMaxRootIters
B2_API int32 b2_toiMaxRootIters
Definition:
b2_time_of_impact.cpp:34
b2_toiTime
B2_API float b2_toiTime
Definition:
b2_time_of_impact.cpp:32
b2_dynamicBody
@ b2_dynamicBody
Definition:
b2_body.h:47
DebugDraw::DrawString
void DrawString(int x, int y, const char *string,...)
Definition:
draw.cpp:772
b2_toiMaxTime
B2_API float b2_toiMaxTime
Definition:
b2_time_of_impact.cpp:32
b2Shape::m_radius
float m_radius
Definition:
b2_shape.h:102
RandomFloat
float RandomFloat()
Random number in range [-1,1].
Definition:
test.h:37
b2PolygonShape::SetAsBox
void SetAsBox(float hx, float hy)
Definition:
b2_polygon_shape.cpp:36
b2_gjkCalls
B2_API int32 b2_gjkCalls
Definition:
b2_distance.cpp:30
b2BodyDef
Definition:
b2_body.h:52
b2_gjkMaxIters
B2_API int32 b2_gjkMaxIters
Definition:
b2_distance.cpp:30
ContinuousTest::Launch
void Launch()
Definition:
continuous_test.cpp:95
Settings
Definition:
settings.h:25
b2World::CreateBody
b2Body * CreateBody(const b2BodyDef *def)
Definition:
b2_world.cpp:115
b2PolygonShape
Definition:
b2_polygon_shape.h:32
RegisterTest
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition:
test.cpp:458
Test::m_textLine
int32 m_textLine
Definition:
test.h:127
ContinuousTest
Definition:
continuous_test.cpp:25
int32
signed int int32
Definition:
b2_types.h:28
Test
Definition:
test.h:80
ContinuousTest::m_angularVelocity
float m_angularVelocity
Definition:
continuous_test.cpp:157
b2BodyDef::position
b2Vec2 position
Definition:
b2_body.h:78
ContinuousTest::Step
void Step(Settings &settings) override
Definition:
continuous_test.cpp:113
Test::Step
virtual void Step(Settings &settings)
Definition:
test.cpp:278
b2_toiIters
B2_API int32 b2_toiIters
Definition:
b2_time_of_impact.cpp:33
b2CircleShape::m_p
b2Vec2 m_p
Position.
Definition:
b2_circle_shape.h:57
Test::m_world
b2World * m_world
Definition:
test.h:128
b2Body::CreateFixture
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition:
b2_body.cpp:165
mvsim
Author(s):
autogenerated on Wed May 28 2025 02:13:07