Skip to content

Commit fa02078

Browse files
committed
Remove the use of "Bodies" i.e. explicitly letting the Sandbox know which bodies were added. It doesn't scale well.
1 parent 098b432 commit fa02078

File tree

47 files changed

+104
-303
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+104
-303
lines changed

LowLevel/Sandbox/Assets/Sandbox/Scripts/SandboxManager.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
public class SandboxManager : MonoBehaviour, IShapeColorProvider
1515
{
16-
public NativeHashSet<PhysicsBody> Bodies;
1716
public ref Random Random => ref m_Random;
1817
public bool WorldPaused { get; private set; }
1918

@@ -187,11 +186,27 @@ public void ResetSceneState()
187186
foreach (var sceneWorld in FindObjectsByType<SceneWorld>(FindObjectsSortMode.None))
188187
sceneWorld.enabled = false;
189188

190-
// Destroy any current bodies.
191-
if (Bodies.IsCreated && Bodies.Count > 0)
192189
{
193-
PhysicsWorld.DestroyBodyBatch(Bodies.ToNativeArray(Allocator.Temp));
194-
Bodies.Clear();
190+
var destroyBodies = new NativeList<PhysicsBody>(1000, Allocator.Temp);
191+
192+
// Iterate all worlds.
193+
using var allWorlds = PhysicsWorld.GetWorlds();
194+
foreach (var world in allWorlds)
195+
{
196+
// Iterate all non-owned bodies.
197+
using var bodies = world.GetBodies();
198+
foreach (var body in bodies)
199+
{
200+
if (!body.isOwned)
201+
destroyBodies.Add(body);
202+
}
203+
}
204+
205+
if (destroyBodies.Length > 0)
206+
PhysicsWorld.DestroyBodyBatch(destroyBodies.AsArray());
207+
208+
// Dispose.
209+
destroyBodies.Dispose();
195210
}
196211

197212
// Clear the debug draw.
@@ -217,19 +232,13 @@ private void OnEnable()
217232
// We don't want this appearing all the time.
218233
UnityEngine.Rendering.DebugManager.instance.enableRuntimeUI = false;
219234

220-
Bodies = new NativeHashSet<PhysicsBody>(500, Allocator.Persistent);
221235
m_DrawFlagElements = new Dictionary<PhysicsWorld.DrawOptions, Toggle>(capacity: 8);
222236

223237
// Overrides.
224238
m_OverrideDrawOptions = PhysicsWorld.DrawOptions.Off;
225239
m_OverridePreviousDrawOptions = PhysicsWorld.DrawOptions.Off;
226240
}
227241

228-
private void OnDisable()
229-
{
230-
Bodies.Dispose();
231-
}
232-
233242
private void Start()
234243
{
235244
m_CameraManipulator = FindFirstObjectByType<CameraManipulator>();

LowLevel/Sandbox/Assets/Scenes/Batching/Shooter/Shooter.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ private void OnPreSimulation(PhysicsWorld world, float timeStep)
154154

155155
// Create the Batch.
156156
{
157-
var worldBodies = m_SandboxManager.Bodies;
158157
ref var random = ref m_SandboxManager.Random;
159158

160159
var capsuleRadius = random.NextFloat(m_BatchRadius.x, m_BatchRadius.y);
@@ -197,8 +196,6 @@ private void OnPreSimulation(PhysicsWorld world, float timeStep)
197196
shapeDef.surfaceMaterial.customColor = m_SandboxManager.ShapeColorState;
198197
var body = bodies[i];
199198
body.CreateShape(capsuleGeometry, shapeDef);
200-
201-
worldBodies.Add(body);
202199
}
203200

204201
// Dispose.

LowLevel/Sandbox/Assets/Scenes/Benchmark/Barrel/Barrel.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ private void SetupScene()
153153
var rightGeometry = PolygonGeometry.Create(vertices: new Vector2[] { new(1.0f, 0f), new(-0.5f, 1f), new(0f, 2f) }.AsSpan());
154154

155155
var world = PhysicsWorld.defaultWorld;
156-
var bodies = m_SandboxManager.Bodies;
157156

158157
ref var random = ref m_SandboxManager.Random;
159158

@@ -178,36 +177,28 @@ private void SetupScene()
178177
case ObjectType.Circle:
179178
{
180179
var body = world.CreateBody(bodyDef);
181-
bodies.Add(body);
182-
183180
CreateCircle(body, shapeDef, ref random);
184181
continue;
185182
}
186183

187184
case ObjectType.Capsule:
188185
{
189186
var body = world.CreateBody(bodyDef);
190-
bodies.Add(body);
191-
192187
CreateCapsule(body, shapeDef, ref random);
193188
continue;
194189
}
195190

196191
case ObjectType.Polygon:
197192
{
198193
var body = world.CreateBody(bodyDef);
199-
bodies.Add(body);
200-
201194
CreatePolygon(body, shapeDef, ref random);
202195
continue;
203196
}
204197

205198
case ObjectType.PrimitiveMix:
206199
{
207200
var body = world.CreateBody(bodyDef);
208-
bodies.Add(body);
209-
210-
switch (bodies.Count % 3)
201+
switch (i % 3)
211202
{
212203
case 0:
213204
{
@@ -235,7 +226,6 @@ private void SetupScene()
235226
case ObjectType.Compound:
236227
{
237228
var body = world.CreateBody(bodyDef);
238-
bodies.Add(body);
239229

240230
body.CreateShape(leftGeometry, shapeDef);
241231
body.CreateShape(rightGeometry, shapeDef);
@@ -245,8 +235,6 @@ private void SetupScene()
245235
case ObjectType.Ragdoll:
246236
{
247237
using var ragdoll = RagdollFactory.Spawn(world, bodyDef.position, ragDollConfiguration, true, ref random);
248-
foreach (var body in ragdoll)
249-
bodies.Add(body);
250238
continue;
251239
}
252240

@@ -290,9 +278,6 @@ private void CreateBarrel()
290278

291279
var body = world.CreateBody(PhysicsBodyDefinition.defaultDefinition);
292280

293-
var bodies = m_SandboxManager.Bodies;
294-
bodies.Add(body);
295-
296281
{
297282
var boxTransform = new PhysicsTransform(new Vector2(0f, 4f), PhysicsRotate.identity);
298283
var boxGeometry = PolygonGeometry.CreateBox(new Vector2(94f, 10f), radius: 0f, transform: boxTransform);

LowLevel/Sandbox/Assets/Scenes/Benchmark/Funnel/Funnel.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ private void SpawnObject()
118118

119119
// Spawn Object.
120120
var world = PhysicsWorld.defaultWorld;
121-
var bodies = m_SandboxManager.Bodies;
122121
ref var random = ref m_SandboxManager.Random;
123122

124123
var spawnPosition = new Vector2(random.NextFloat(-SpawnSide, SpawnSide), 35f);
@@ -135,51 +134,46 @@ private void SpawnObject()
135134
case ObjectType.Circle:
136135
{
137136
var body = world.CreateBody(bodyDef);
138-
bodies.Add(body);
139137
CreateCircle(body, shapeDef, ref random);
140138
return;
141139
}
142140

143141
case ObjectType.Capsule:
144142
{
145143
var body = world.CreateBody(bodyDef);
146-
bodies.Add(body);
147144
CreateCapsule(body, shapeDef, ref random);
148145
return;
149146
}
150147

151148
case ObjectType.Polygon:
152149
{
153150
var body = world.CreateBody(bodyDef);
154-
bodies.Add(body);
155151
CreatePolygon(body, shapeDef, ref random);
156152
return;
157153
}
158154

159155
case ObjectType.Compound:
160156
{
161157
var body = world.CreateBody(bodyDef);
162-
bodies.Add(body);
163158
CreateCompound(body, shapeDef);
164159
return;
165160
}
166161

167162
case ObjectType.Ragdoll:
168163
{
169-
CreateRagdoll(world, spawnPosition, random, bodies);
164+
CreateRagdoll(world, spawnPosition, random);
170165
return;
171166
}
172167

173168
case ObjectType.Softbody:
174169
{
175-
CreateDonut(world, spawnPosition, bodies);
170+
CreateDonut(world, spawnPosition);
176171
return;
177172
}
178173

179174
case ObjectType.Random:
180175
{
181176
var body = world.CreateBody(bodyDef);
182-
bodies.Add(body);
183177

184178
switch (random.NextInt(0, 7))
185179
{
@@ -209,13 +203,13 @@ private void SpawnObject()
209203

210204
case 5:
211205
{
212-
CreateRagdoll(world, spawnPosition, random, bodies);
206+
CreateRagdoll(world, spawnPosition, random);
213207
return;
214208
}
215209

216210
case 6:
217211
{
218-
CreateDonut(world, spawnPosition, bodies);
212+
CreateDonut(world, spawnPosition);
219213
return;
220214
}
221215
}
@@ -255,7 +249,7 @@ private void CreatePolygon(PhysicsBody body, PhysicsShapeDefinition shapeDef, re
255249
body.CreateShape(polygonGeometry, shapeDef);
256250
}
257251

258-
private void CreateRagdoll(PhysicsWorld world, Vector2 spawnPosition, Random random, NativeHashSet<PhysicsBody> bodies)
252+
private void CreateRagdoll(PhysicsWorld world, Vector2 spawnPosition, Random random)
259253
{
260254
var ragDollConfiguration = new RagdollFactory.Configuration
261255
{
@@ -275,15 +269,11 @@ private void CreateRagdoll(PhysicsWorld world, Vector2 spawnPosition, Random ran
275269
};
276270

277271
using var ragdoll = RagdollFactory.Spawn(world, spawnPosition, ragDollConfiguration, true, ref random);
278-
foreach (var body in ragdoll)
279-
bodies.Add(body);
280272
}
281273

282-
private void CreateDonut(PhysicsWorld world, Vector2 spawnPosition, NativeHashSet<PhysicsBody> bodies)
274+
private void CreateDonut(PhysicsWorld world, Vector2 spawnPosition)
283275
{
284276
using var donut = SoftbodyFactory.SpawnDonut(world, m_SandboxManager, spawnPosition, sides: 7, scale: m_ObjectScale * 0.5f, triggerEvents: true, jointFrequency: 20f);
285-
foreach (var body in donut)
286-
bodies.Add(body);
287277
}
288278

289279
private void CreateCompound(PhysicsBody body, PhysicsShapeDefinition shapeDef)
@@ -354,12 +344,10 @@ private void SetupScene()
354344
m_SpawnTime = 0f;
355345

356346
var world = PhysicsWorld.defaultWorld;
357-
var bodies = m_SandboxManager.Bodies;
358347

359348
// Ground.
360349
{
361350
var groundBody = world.CreateBody(PhysicsBodyDefinition.defaultDefinition);
362-
bodies.Add(groundBody);
363351

364352
using var points = new NativeList<Vector2>(Allocator.Temp)
365353
{
@@ -385,7 +373,6 @@ private void SetupScene()
385373
bodyDef.position = new Vector2(0f, y);
386374

387375
var body = world.CreateBody(bodyDef);
388-
bodies.Add(body);
389376

390377
var boxGeometry = PolygonGeometry.CreateBox(new Vector2(11f, 1f), radius: 0.5f);
391378
var shapeDef = new PhysicsShapeDefinition { density = 1f, surfaceMaterial = new PhysicsShape.SurfaceMaterial { friction = 0.1f, bounciness = 1f } };

LowLevel/Sandbox/Assets/Scenes/Benchmark/JointGrid/JointGrid.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ private void SetupScene()
9494
m_SandboxManager.ResetSceneState();
9595

9696
var world = PhysicsWorld.defaultWorld;
97-
var bodies = m_SandboxManager.Bodies;
9897

9998
var bodyDef = PhysicsBodyDefinition.defaultDefinition;
10099
var hingeJointDef = PhysicsHingeJointDefinition.defaultDefinition;
@@ -150,11 +149,6 @@ private void SetupScene()
150149
}
151150
}
152151

153-
foreach (var body in bodyArray)
154-
{
155-
bodies.Add(body);
156-
}
157-
158152
bodyArray.Dispose();
159153
}
160154
}

LowLevel/Sandbox/Assets/Scenes/Benchmark/LargeCompound/LargeCompound.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ private void SetupScene()
6464
m_SandboxManager.ResetSceneState();
6565

6666
var world = PhysicsWorld.defaultWorld;
67-
var bodies = m_SandboxManager.Bodies;
6867

6968
const int height = 300;
7069

@@ -74,7 +73,6 @@ private void SetupScene()
7473
// Ground.
7574
{
7675
var body = world.CreateBody(PhysicsBodyDefinition.defaultDefinition);
77-
bodies.Add(body);
7876
var shapeDef = PhysicsShapeDefinition.defaultDefinition;
7977

8078
for (var i = 0; i < height; ++i)
@@ -104,7 +102,6 @@ private void SetupScene()
104102
var bodyX = -0.5f * gridSize * m_CompoundCount * span + n * span * gridSize;
105103
bodyDef.position = new Vector2(bodyX, bodyY);
106104
var body = world.CreateBody(bodyDef);
107-
bodies.Add(body);
108105

109106
for (var i = 0; i < span; ++i)
110107
{

LowLevel/Sandbox/Assets/Scenes/Benchmark/LargeKinematic/LargeKinematic.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,13 @@ private void SetupScene()
8888
m_SandboxManager.ResetSceneState();
8989

9090
var world = PhysicsWorld.defaultWorld;
91-
var bodies = m_SandboxManager.Bodies;
9291

9392
// Rotating Kinematic.
9493
{
9594
var bodyDef = new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Kinematic, angularVelocity = m_AngularVelocity };
9695
var shapeDef = new PhysicsShapeDefinition { startMassUpdate = false };
9796

9897
var body = world.CreateBody(bodyDef);
99-
bodies.Add(body);
10098

10199
const float grid = 1f;
102100
var gridBox = new Vector2(grid, grid);

LowLevel/Sandbox/Assets/Scenes/Benchmark/LargePyramid/LargePyramid.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,10 @@ private void SetupScene()
8686
m_SandboxManager.ResetSceneState();
8787

8888
var world = PhysicsWorld.defaultWorld;
89-
var bodies = m_SandboxManager.Bodies;
9089

9190
// Ground.
9291
{
9392
var body = world.CreateBody(new PhysicsBodyDefinition { position = new Vector2(0f, -1f) });
94-
bodies.Add(body);
9593

9694
const float groundLength = 1000f;
9795
body.CreateShape(PolygonGeometry.CreateBox(new Vector2(groundLength, 2f)), PhysicsShapeDefinition.defaultDefinition);
@@ -121,7 +119,6 @@ private void SetupScene()
121119

122120
bodyDef.position = new Vector2(x, y);
123121
var body = world.CreateBody(bodyDef);
124-
bodies.Add(body);
125122

126123
shapeDef.surfaceMaterial.customColor = m_SandboxManager.ShapeColorState;
127124
body.CreateShape(boxGeometry, shapeDef);

0 commit comments

Comments
 (0)