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 
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.0f);
58  ground->CreateFixture(&shape, 0.0f);
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.0f);
76  ground->CreateFixture(&shape, 0.0f);
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.0f, 0.75f);
90  b2Vec2 y;
91  b2Vec2 deltaX(0.5625f, 1.25f);
92  b2Vec2 deltaY(1.125f, 0.0f);
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.0f);
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  {
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.0f));
133  g_debugDraw.DrawString(5, m_textLine, "dynamic tree height = %d, min = %d", height, int32(minimumHeight));
135 
136  Test::Step(settings);
137 
138  g_debugDraw.DrawString(5, m_textLine, "create time = %6.2f ms, fixture count = %d",
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 
157 };
158 
159 static int testIndex = RegisterTest("Benchmark", "Tiles", Tiles::Create);
static int testIndex
Definition: tiles.cpp:159
b2Fixture * CreateFixture(const b2FixtureDef *def)
Definition: b2_body.cpp:165
const b2ContactManager & GetContactManager() const
Get the contact manager for testing.
Definition: b2_world.h:335
float m_createTime
Definition: tiles.cpp:156
f
float x
Definition: b2_math.h:128
float y
Definition: b2_math.h:128
int32 m_textLine
Definition: test.h:127
Tiles()
Definition: tiles.cpp:35
Definition: test.h:80
Definition: tiles.cpp:27
A 2D column vector.
Definition: b2_math.h:41
signed int int32
Definition: b2_types.h:28
void SetAsBox(float hx, float hy)
int32 GetProxyCount() const
Get the number of proxies.
int32 m_fixtureCount
Definition: tiles.cpp:155
b2BodyType type
Definition: b2_body.h:74
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
int32 GetTreeHeight() const
Get the height of the embedded tree.
int32 m_textIncrement
Definition: test.h:135
b2World * m_world
Definition: test.h:128
void Step(Settings &settings) override
Definition: tiles.cpp:126
b2Vec2 position
Definition: b2_body.h:78
b2BroadPhase m_broadPhase
int RegisterTest(const char *category, const char *name, TestCreateFcn *fcn)
Definition: test.cpp:458
void DrawString(int x, int y, const char *string,...)
Definition: draw.cpp:772
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
static Test * Create()
Definition: tiles.cpp:150
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