-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBenchmarkBarrel.cs
More file actions
352 lines (293 loc) · 11.9 KB
/
BenchmarkBarrel.cs
File metadata and controls
352 lines (293 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System.Numerics;
using Box2D.NET.Shared;
using ImGuiNET;
using static Box2D.NET.B2Ids;
using static Box2D.NET.B2Hulls;
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.Shared.Humans;
using static Box2D.NET.Shared.RandomSupports;
namespace Box2D.NET.Samples.Samples.Benchmarks;
// Note: resetting the scene is non-deterministic because the world uses freelists
public class BenchmarkBarrel : Sample
{
private static readonly int SampleBenchmarkBarrel = SampleFactory.Shared.RegisterSample("Benchmark", "Barrel", Create);
public enum ShapeType
{
e_circleShape = 0,
e_capsuleShape,
e_mixShape,
e_compoundShape,
e_humanShape,
}
private const int e_maxColumns = 30;
private const int e_maxRows = 300;
private B2BodyId[] m_bodies = new B2BodyId[e_maxRows * e_maxColumns];
private Human[] m_humans = new Human[e_maxRows * e_maxColumns];
private int m_columnCount;
private int m_rowCount;
private ShapeType m_shapeType;
private static Sample Create(SampleContext context)
{
return new BenchmarkBarrel(context);
}
public BenchmarkBarrel(SampleContext context) : base(context)
{
if (m_context.restart == false)
{
m_camera.center = new B2Vec2(8.0f, 53.0f);
m_camera.zoom = 25.0f * 2.35f;
}
m_context.debugDraw.drawJoints = false;
{
float gridSize = 1.0f;
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
B2ShapeDef shapeDef = b2DefaultShapeDef();
float y = 0.0f;
float x = -40.0f * gridSize;
for (int i = 0; i < 81; ++i)
{
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
b2CreatePolygonShape(groundId, shapeDef, box);
x += gridSize;
}
y = gridSize;
x = -40.0f * gridSize;
for (int i = 0; i < 100; ++i)
{
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
b2CreatePolygonShape(groundId, shapeDef, box);
y += gridSize;
}
y = gridSize;
x = 40.0f * gridSize;
for (int i = 0; i < 100; ++i)
{
B2Polygon box = b2MakeOffsetBox(0.5f * gridSize, 0.5f * gridSize, new B2Vec2(x, y), b2Rot_identity);
b2CreatePolygonShape(groundId, shapeDef, box);
y += gridSize;
}
B2Segment segment = new B2Segment(new B2Vec2(-800.0f, -80.0f), new B2Vec2(800.0f, -80.0f));
b2CreateSegmentShape(groundId, shapeDef, segment);
}
for (int i = 0; i < e_maxRows * e_maxColumns; ++i)
{
m_bodies[i] = b2_nullBodyId;
}
//memset( m_humans, 0, sizeof( m_humans ) );
for (int i = 0; i < m_humans.Length; ++i)
{
m_humans[i] = new Human();
m_humans[i].Clear();
}
m_shapeType = ShapeType.e_compoundShape;
m_columnCount = e_maxColumns / (m_isDebug ? 3 : 2);
m_rowCount = e_maxRows / (m_isDebug ? 3 : 2);
CreateScene();
}
void CreateScene()
{
g_randomSeed = 42;
for (int i = 0; i < e_maxRows * e_maxColumns; ++i)
{
if (B2_IS_NON_NULL(m_bodies[i]))
{
b2DestroyBody(m_bodies[i]);
m_bodies[i] = b2_nullBodyId;
}
if (m_humans[i].isSpawned)
{
DestroyHuman(ref m_humans[i]);
}
}
// if (m_shapeType == ShapeType.e_compoundShape)
// {
// if (m_context.sampleDebug == false)
// {
// m_columnCount = e_maxColumns;
// }
// }
// else if (m_shapeType == ShapeType.e_humanShape)
// {
// if (m_context.sampleDebug)
// {
// m_rowCount = 5;
// m_columnCount = 10;
// }
// else
// {
// m_rowCount = 30;
// }
// }
float rad = 0.5f;
float shift = 1.15f;
float centerx = shift * m_columnCount / 2.0f;
float centery = shift / 2.0f;
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
// todo eliminate this once rolling resistance is added
if (m_shapeType == ShapeType.e_mixShape)
{
bodyDef.angularDamping = 0.3f;
}
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 1.0f;
shapeDef.material.friction = 0.5f;
B2Capsule capsule = new B2Capsule(new B2Vec2(0.0f, -0.25f), new B2Vec2(0.0f, 0.25f), rad);
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), rad);
B2Vec2[] points = new B2Vec2[3] { new B2Vec2(-0.1f, -0.5f), new B2Vec2(0.1f, -0.5f), new B2Vec2(0.0f, 0.5f) };
B2Hull wedgeHull = b2ComputeHull(points, 3);
B2Polygon wedge = b2MakePolygon(wedgeHull, 0.0f);
B2Vec2[] vertices = new B2Vec2[3];
vertices[0] = new B2Vec2(-1.0f, 0.0f);
vertices[1] = new B2Vec2(0.5f, 1.0f);
vertices[2] = new B2Vec2(0.0f, 2.0f);
B2Hull hull = b2ComputeHull(vertices, 3);
B2Polygon left = b2MakePolygon(hull, 0.0f);
vertices[0] = new B2Vec2(1.0f, 0.0f);
vertices[1] = new B2Vec2(-0.5f, 1.0f);
vertices[2] = new B2Vec2(0.0f, 2.0f);
hull = b2ComputeHull(vertices, 3);
B2Polygon right = b2MakePolygon(hull, 0.0f);
// b2Polygon top = b2MakeOffsetBox(0.8f, 0.2f, {0.0f, 0.8f}, 0.0f);
// b2Polygon leftLeg = b2MakeOffsetBox(0.2f, 0.5f, {-0.6f, 0.5f}, 0.0f);
// b2Polygon rightLeg = b2MakeOffsetBox(0.2f, 0.5f, {0.6f, 0.5f}, 0.0f);
float side = -0.1f;
float extray = 0.5f;
if (m_shapeType == ShapeType.e_compoundShape)
{
extray = 0.25f;
side = 0.25f;
shift = 2.0f;
centerx = shift * m_columnCount / 2.0f - 1.0f;
}
else if (m_shapeType == ShapeType.e_humanShape)
{
extray = 0.5f;
side = 0.55f;
shift = 2.5f;
centerx = shift * m_columnCount / 2.0f;
}
int index = 0;
float yStart = m_shapeType == ShapeType.e_humanShape ? 2.0f : 100.0f;
for (int i = 0; i < m_columnCount; ++i)
{
float x = i * shift - centerx;
for (int j = 0; j < m_rowCount; ++j)
{
float y = j * (shift + extray) + centery + yStart;
bodyDef.position = new B2Vec2(x + side, y);
side = -side;
if (m_shapeType == ShapeType.e_circleShape)
{
m_bodies[index] = b2CreateBody(m_worldId, bodyDef);
circle.radius = RandomFloatRange(0.25f, 0.75f);
shapeDef.material.rollingResistance = 0.2f;
b2CreateCircleShape(m_bodies[index], shapeDef, circle);
}
else if (m_shapeType == ShapeType.e_capsuleShape)
{
m_bodies[index] = b2CreateBody(m_worldId, bodyDef);
capsule.radius = RandomFloatRange(0.25f, 0.5f);
float length = RandomFloatRange(0.25f, 1.0f);
capsule.center1 = new B2Vec2(0.0f, -0.5f * length);
capsule.center2 = new B2Vec2(0.0f, 0.5f * length);
shapeDef.material.rollingResistance = 0.2f;
b2CreateCapsuleShape(m_bodies[index], shapeDef, capsule);
}
else if (m_shapeType == ShapeType.e_mixShape)
{
m_bodies[index] = b2CreateBody(m_worldId, bodyDef);
int mod = index % 3;
if (mod == 0)
{
circle.radius = RandomFloatRange(0.25f, 0.75f);
b2CreateCircleShape(m_bodies[index], shapeDef, circle);
}
else if (mod == 1)
{
capsule.radius = RandomFloatRange(0.25f, 0.5f);
float length = RandomFloatRange(0.25f, 1.0f);
capsule.center1 = new B2Vec2(0.0f, -0.5f * length);
capsule.center2 = new B2Vec2(0.0f, 0.5f * length);
b2CreateCapsuleShape(m_bodies[index], shapeDef, capsule);
}
else if (mod == 2)
{
float width = RandomFloatRange(0.1f, 0.5f);
float height = RandomFloatRange(0.5f, 0.75f);
B2Polygon box = b2MakeBox(width, height);
// Don't put a function call into a macro.
float value = RandomFloatRange(-1.0f, 1.0f);
box.radius = 0.25f * b2MaxFloat(0.0f, value);
b2CreatePolygonShape(m_bodies[index], shapeDef, box);
}
else
{
wedge.radius = RandomFloatRange(0.1f, 0.25f);
b2CreatePolygonShape(m_bodies[index], shapeDef, wedge);
}
}
else if (m_shapeType == ShapeType.e_compoundShape)
{
m_bodies[index] = b2CreateBody(m_worldId, bodyDef);
b2CreatePolygonShape(m_bodies[index], shapeDef, left);
b2CreatePolygonShape(m_bodies[index], shapeDef, right);
// b2CreatePolygonShape(m_bodies[index], &shapeDef, &top);
// b2CreatePolygonShape(m_bodies[index], &shapeDef, &leftLeg);
// b2CreatePolygonShape(m_bodies[index], &shapeDef, &rightLeg);
}
else if (m_shapeType == ShapeType.e_humanShape)
{
float scale = 3.5f;
float jointFriction = 0.05f;
float jointHertz = 5.0f;
float jointDamping = 0.5f;
CreateHuman(ref m_humans[index], m_worldId, bodyDef.position, scale, jointFriction, jointHertz, jointDamping, index + 1, B2UserData.Empty, false);
}
index += 1;
}
}
}
public override void UpdateGui()
{
base.UpdateGui();
float fontSize = ImGui.GetFontSize();
float height = 10.0f * fontSize;
ImGui.SetNextWindowPos(new Vector2(0.5f * fontSize, m_camera.height - height - 2.0f * fontSize), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(15.0f * fontSize, height));
ImGui.Begin("Benchmark: Barrel", ImGuiWindowFlags.NoResize);
bool changed = false;
if (ImGui.SliderInt("rows", ref m_rowCount, 1, e_maxRows, "%d"))
{
changed = true;
}
if (ImGui.SliderInt("columns", ref m_columnCount, 1, e_maxColumns, "%d"))
{
changed = true;
}
string[] shapeTypes = ["Circle", "Capsule", "Mix", "Compound", "Human"];
int shapeType = (int)m_shapeType;
if (ImGui.Combo("Shape", ref shapeType, shapeTypes, shapeTypes.Length))
{
changed = true;
}
m_shapeType = (ShapeType)shapeType;
if (ImGui.Button("Reset Scene"))
{
changed = true;
}
if (changed)
{
CreateScene();
}
ImGui.End();
}
}