1 /*
2 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef TILES_H
20 #define TILES_H
21 
22 /// This stress tests the dynamic tree broad-phase. This also shows that tile
23 /// based collision is _not_ smooth due to Box2D not knowing about adjacency.
24 class Tiles : public Test
25 {
26 public:
27 	enum
28 	{
29 		e_count = 20
30 	};
31 
Tiles()32 	Tiles()
33 	{
34 		m_fixtureCount = 0;
35 		b2Timer timer;
36 
37 		{
38 			float32 a = 0.5f;
39 			b2BodyDef bd;
40 			bd.position.y = -a;
41 			b2Body* ground = m_world->CreateBody(&bd);
42 
43 #if 1
44 			int32 N = 200;
45 			int32 M = 10;
46 			b2Vec2 position;
47 			position.y = 0.0f;
48 			for (int32 j = 0; j < M; ++j)
49 			{
50 				position.x = -N * a;
51 				for (int32 i = 0; i < N; ++i)
52 				{
53 					b2PolygonShape shape;
54 					shape.SetAsBox(a, a, position, 0.0f);
55 					ground->CreateFixture(&shape, 0.0f);
56 					++m_fixtureCount;
57 					position.x += 2.0f * a;
58 				}
59 				position.y -= 2.0f * a;
60 			}
61 #else
62 			int32 N = 200;
63 			int32 M = 10;
64 			b2Vec2 position;
65 			position.x = -N * a;
66 			for (int32 i = 0; i < N; ++i)
67 			{
68 				position.y = 0.0f;
69 				for (int32 j = 0; j < M; ++j)
70 				{
71 					b2PolygonShape shape;
72 					shape.SetAsBox(a, a, position, 0.0f);
73 					ground->CreateFixture(&shape, 0.0f);
74 					position.y -= 2.0f * a;
75 				}
76 				position.x += 2.0f * a;
77 			}
78 #endif
79 		}
80 
81 		{
82 			float32 a = 0.5f;
83 			b2PolygonShape shape;
84 			shape.SetAsBox(a, a);
85 
86 			b2Vec2 x(-7.0f, 0.75f);
87 			b2Vec2 y;
88 			b2Vec2 deltaX(0.5625f, 1.25f);
89 			b2Vec2 deltaY(1.125f, 0.0f);
90 
91 			for (int32 i = 0; i < e_count; ++i)
92 			{
93 				y = x;
94 
95 				for (int32 j = i; j < e_count; ++j)
96 				{
97 					b2BodyDef bd;
98 					bd.type = b2_dynamicBody;
99 					bd.position = y;
100 
101 					//if (i == 0 && j == 0)
102 					//{
103 					//	bd.allowSleep = false;
104 					//}
105 					//else
106 					//{
107 					//	bd.allowSleep = true;
108 					//}
109 
110 					b2Body* body = m_world->CreateBody(&bd);
111 					body->CreateFixture(&shape, 5.0f);
112 					++m_fixtureCount;
113 					y += deltaY;
114 				}
115 
116 				x += deltaX;
117 			}
118 		}
119 
120 		m_createTime = timer.GetMilliseconds();
121 	}
122 
Step(Settings * settings)123 	void Step(Settings* settings)
124 	{
125 		const b2ContactManager& cm = m_world->GetContactManager();
126 		int32 height = cm.m_broadPhase.GetTreeHeight();
127 		int32 leafCount = cm.m_broadPhase.GetProxyCount();
128 		int32 minimumNodeCount = 2 * leafCount - 1;
129 		float32 minimumHeight = ceilf(logf(float32(minimumNodeCount)) / logf(2.0f));
130 		g_debugDraw.DrawString(5, m_textLine, "dynamic tree height = %d, min = %d", height, int32(minimumHeight));
131 		m_textLine += DRAW_STRING_NEW_LINE;
132 
133 		Test::Step(settings);
134 
135 		g_debugDraw.DrawString(5, m_textLine, "create time = %6.2f ms, fixture count = %d",
136 			m_createTime, m_fixtureCount);
137 		m_textLine += DRAW_STRING_NEW_LINE;
138 
139 		//b2DynamicTree* tree = &m_world->m_contactManager.m_broadPhase.m_tree;
140 
141 		//if (m_stepCount == 400)
142 		//{
143 		//	tree->RebuildBottomUp();
144 		//}
145 	}
146 
Create()147 	static Test* Create()
148 	{
149 		return new Tiles;
150 	}
151 
152 	int32 m_fixtureCount;
153 	float32 m_createTime;
154 };
155 
156 #endif
157