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 
30  {
31  {
32  b2BodyDef bd;
33  bd.position.Set(0.0f, 0.0f);
34  b2Body* body = m_world->CreateBody(&bd);
35 
36  b2EdgeShape edge;
37 
38  edge.SetTwoSided(b2Vec2(-10.0f, 0.0f), b2Vec2(10.0f, 0.0f));
39  body->CreateFixture(&edge, 0.0f);
40 
41  b2PolygonShape shape;
42  shape.SetAsBox(0.2f, 1.0f, b2Vec2(0.5f, 1.0f), 0.0f);
43  body->CreateFixture(&shape, 0.0f);
44  }
45 
46 #if 1
47  {
48  b2BodyDef bd;
49  bd.type = b2_dynamicBody;
50  bd.position.Set(0.0f, 20.0f);
51  //bd.angle = 0.1f;
52 
53  b2PolygonShape shape;
54  shape.SetAsBox(2.0f, 0.1f);
55 
56  m_body = m_world->CreateBody(&bd);
57  m_body->CreateFixture(&shape, 1.0f);
58 
59  m_angularVelocity = RandomFloat(-50.0f, 50.0f);
60  //m_angularVelocity = 46.661274f;
61  m_body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
63  }
64 #else
65  {
66  b2BodyDef bd;
67  bd.type = b2_dynamicBody;
68  bd.position.Set(0.0f, 2.0f);
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.0f);
75 
76  bd.bullet = true;
77  bd.position.Set(0.0f, 10.0f);
78  body = m_world->CreateBody(&bd);
79  body->CreateFixture(&shape, 1.0f);
80  body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
81  }
82 #endif
83 
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  {
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.0f, 20.0f), 0.0f);
108  m_angularVelocity = RandomFloat(-50.0f, 50.0f);
109  m_body->SetLinearVelocity(b2Vec2(0.0f, -100.0f));
111  }
112 
113  void Step(Settings& settings) override
114  {
115  Test::Step(settings);
116 
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);
124  }
125 
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);
135 
136  g_debugDraw.DrawString(5, m_textLine, "ave [max] toi root iters = %3.1f [%d]",
137  b2_toiRootIters / float(b2_toiCalls), b2_toiMaxRootIters);
139 
140  g_debugDraw.DrawString(5, m_textLine, "ave [max] toi time = %.1f [%.1f] (microseconds)",
141  1000.0f * b2_toiTime / float(b2_toiCalls), 1000.0f * b2_toiMaxTime);
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 
158 };
159 
160 static int testIndex = RegisterTest("Continuous", "Continuous Test", ContinuousTest::Create);
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
f
#define B2_API
Definition: b2_api.h:49
B2_API int32 b2_toiCalls
B2_API int32 b2_gjkMaxIters
Definition: b2_distance.cpp:30
void SetTransform(const b2Vec2 &position, float angle)
Definition: b2_body.cpp:415
int32 m_textLine
Definition: test.h:127
B2_API int32 b2_gjkCalls
Definition: b2_distance.cpp:30
void Step(Settings &settings) override
Definition: test.h:80
bool bullet
Definition: b2_body.h:115
float RandomFloat()
Random number in range [-1,1].
Definition: test.h:37
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
A solid circle shape.
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
b2Vec2 m_p
Position.
void SetTwoSided(const b2Vec2 &v1, const b2Vec2 &v2)
Set this as an isolated edge. Collision is two-sided.
void SetAsBox(float hx, float hy)
B2_API int32 b2_toiMaxRootIters
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
void SetLinearVelocity(const b2Vec2 &v)
Definition: b2_body.h:504
void Set(float x_, float y_)
Set this vector to some specified coordinates.
Definition: b2_math.h:53
static int testIndex
B2_API int32 b2_gjkIters
Definition: b2_distance.cpp:30
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
static Test * Create()
B2_API int32 b2_toiRootIters
int32 m_stepCount
Definition: test.h:134
b2Vec2 position
Definition: b2_body.h:78
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void SetAngularVelocity(float omega)
Definition: b2_body.h:524
B2_API float b2_toiTime
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
B2_API float b2_toiMaxTime
B2_API int32 b2_toiIters
virtual void Step(Settings &settings)
Definition: test.cpp:278
DebugDraw g_debugDraw
Definition: draw.cpp:32
b2Body * CreateBody(const b2BodyDef *def)
Definition: b2_world.cpp:115


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