-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBenchmarkSensor.cs
More file actions
246 lines (200 loc) · 7.61 KB
/
BenchmarkSensor.cs
File metadata and controls
246 lines (200 loc) · 7.61 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
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT
using System.Collections.Generic;
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.RandomSupports;
using static Box2D.NET.B2Worlds;
using static Box2D.NET.B2Diagnostics;
namespace Box2D.NET.Samples.Samples.Benchmarks;
public class BenchmarkSensor : Sample
{
private static readonly int benchmarkSensor = SampleFactory.Shared.RegisterSample("Benchmark", "Sensor", Create);
public class ShapeUserData
{
public int row;
public bool active;
};
private const int m_columnCount = 40;
private const int m_rowCount = 40;
private int m_maxBeginCount;
private int m_maxEndCount;
private ShapeUserData[] m_passiveSensors = new ShapeUserData[m_rowCount];
private ShapeUserData m_activeSensor = new ShapeUserData();
private int m_lastStepCount;
private int m_filterRow;
private static Sample Create(SampleContext context)
{
return new BenchmarkSensor(context);
}
public BenchmarkSensor(SampleContext context) : base(context)
{
if (m_context.restart == false)
{
m_camera.center = new B2Vec2(0.0f, 105.0f);
m_camera.zoom = 125.0f;
}
b2World_SetCustomFilterCallback(m_worldId, FilterFcn, this);
m_activeSensor.row = 0;
m_activeSensor.active = true;
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, bodyDef);
{
float gridSize = 3.0f;
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.isSensor = true;
shapeDef.enableSensorEvents = true;
shapeDef.userData = B2UserData.Ref(m_activeSensor);
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;
}
}
g_randomSeed = 42;
{
float shift = 5.0f;
float xCenter = 0.5f * shift * m_columnCount;
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.isSensor = true;
shapeDef.enableSensorEvents = true;
float yStart = 10.0f;
m_filterRow = m_rowCount >> 1;
for (int j = 0; j < m_rowCount; ++j)
{
m_passiveSensors[j] = new ShapeUserData();
m_passiveSensors[j].row = j;
m_passiveSensors[j].active = false;
shapeDef.userData = B2UserData.Ref(m_passiveSensors[j]);
if (j == m_filterRow)
{
shapeDef.enableCustomFiltering = true;
shapeDef.material.customColor = (uint)B2HexColor.b2_colorFuchsia;
}
else
{
shapeDef.enableCustomFiltering = false;
shapeDef.material.customColor = 0;
}
float y = j * shift + yStart;
for (int i = 0; i < m_columnCount; ++i)
{
float x = i * shift - xCenter;
B2Polygon box = b2MakeOffsetRoundedBox(0.5f, 0.5f, new B2Vec2(x, y), b2Rot_identity, 0.1f);
b2CreatePolygonShape(groundId, shapeDef, box);
}
}
}
m_maxBeginCount = 0;
m_maxEndCount = 0;
m_lastStepCount = 0;
}
void CreateRow(float y)
{
float shift = 5.0f;
float xCenter = 0.5f * shift * m_columnCount;
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.gravityScale = 0.0f;
bodyDef.linearVelocity = new B2Vec2(0.0f, -5.0f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.enableSensorEvents = true;
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.5f);
for (int i = 0; i < m_columnCount; ++i)
{
// stagger bodies to avoid bunching up events into a single update
float yOffset = RandomFloatRange(-1.0f, 1.0f);
bodyDef.position = new B2Vec2(shift * i - xCenter, y + yOffset);
B2BodyId bodyId = b2CreateBody(m_worldId, bodyDef);
b2CreateCircleShape(bodyId, shapeDef, circle);
}
}
private bool Filter(in B2ShapeId idA, in B2ShapeId idB)
{
ShapeUserData userData = null;
if (b2Shape_IsSensor(idA))
{
userData = b2Shape_GetUserData(idA).GetRef<ShapeUserData>();
}
else if (b2Shape_IsSensor(idB))
{
userData = b2Shape_GetUserData(idB).GetRef<ShapeUserData>();
}
if (userData != null)
{
return userData.active == true || userData.row != m_filterRow;
}
return true;
}
private static bool FilterFcn(in B2ShapeId idA, in B2ShapeId idB, object context)
{
BenchmarkSensor self = context as BenchmarkSensor;
return self.Filter(idA, idB);
}
public override void Step()
{
base.Step();
if (m_stepCount == m_lastStepCount)
{
return;
}
HashSet<B2BodyId> zombies = new HashSet<B2BodyId>();
B2SensorEvents events = b2World_GetSensorEvents(m_worldId);
for (int i = 0; i < events.beginCount; ++i)
{
ref B2SensorBeginTouchEvent @event = ref events.beginEvents[i];
// shapes on begin touch are always valid
ShapeUserData userData = b2Shape_GetUserData(@event.sensorShapeId).GetRef<ShapeUserData>();
if (userData.active)
{
zombies.Add(b2Shape_GetBody(@event.visitorShapeId));
}
else
{
// Check custom filter correctness
B2_ASSERT(userData.row != m_filterRow);
// Modify color while overlapped with a sensor
B2SurfaceMaterial surfaceMaterial = b2Shape_GetSurfaceMaterial(@event.visitorShapeId);
surfaceMaterial.customColor = (uint)B2HexColor.b2_colorLime;
b2Shape_SetSurfaceMaterial(@event.visitorShapeId, surfaceMaterial);
}
}
for (int i = 0; i < events.endCount; ++i)
{
ref B2SensorEndTouchEvent @event = ref events.endEvents[i];
if (b2Shape_IsValid(@event.visitorShapeId) == false)
{
continue;
}
// Restore color to default
B2SurfaceMaterial surfaceMaterial = b2Shape_GetSurfaceMaterial(@event.visitorShapeId);
surfaceMaterial.customColor = 0;
b2Shape_SetSurfaceMaterial(@event.visitorShapeId, surfaceMaterial);
}
foreach (B2BodyId bodyId in zombies)
{
b2DestroyBody(bodyId);
}
int delay = 0x1F;
if ((m_stepCount & delay) == 0)
{
CreateRow(10.0f + m_rowCount * 5.0f);
}
m_lastStepCount = m_stepCount;
m_maxBeginCount = b2MaxInt(events.beginCount, m_maxBeginCount);
m_maxEndCount = b2MaxInt(events.endCount, m_maxEndCount);
}
public override void Draw()
{
base.Draw();
DrawTextLine($"max begin touch events = {m_maxBeginCount}");
DrawTextLine($"max end touch events = {m_maxEndCount}");
}
}