Skip to content

Commit 28f2572

Browse files
committed
Add "Compound" scene.
1 parent 1ef8bc2 commit 28f2572

File tree

10 files changed

+550
-1
lines changed

10 files changed

+550
-1
lines changed

LowLevel/Sandbox/Assets/Sandbox.unity

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ MonoBehaviour:
395395
Name: Chain Shape
396396
Category: Shapes
397397
Description: Demonstrates how a Chain Shape doesn't produce "ghost" collisions.
398+
- ScenePath: Assets/Scenes/Shapes/Compound/Compound.unity
399+
Name: Compound
400+
Category: Shapes
401+
Description: Demonstrates how multiple shapes on a single body produce a compound
402+
shape and how to mitigate overlaps.
398403
- ScenePath: Assets/Scenes/Shapes/EllipsePolygons/EllipsePolygons.unity
399404
Name: Ellipse Polygons
400405
Category: Shapes

LowLevel/Sandbox/Assets/Scenes/Shapes/Compound.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
using Unity.Collections;
2+
using UnityEngine;
3+
using UnityEngine.LowLevelPhysics2D;
4+
using UnityEngine.UIElements;
5+
6+
public class Compound : MonoBehaviour
7+
{
8+
private SandboxManager m_SandboxManager;
9+
private SceneManifest m_SceneManifest;
10+
private UIDocument m_UIDocument;
11+
private CameraManipulator m_CameraManipulator;
12+
13+
private PhysicsWorld m_PhysicsWorld;
14+
private Button m_IntrudeShapeButton;
15+
16+
private PhysicsBody m_Table1;
17+
private PhysicsBody m_Table2;
18+
private PhysicsBody m_Ship1;
19+
private PhysicsBody m_Ship2;
20+
21+
private void OnEnable()
22+
{
23+
m_SandboxManager = FindFirstObjectByType<SandboxManager>();
24+
m_SceneManifest = FindFirstObjectByType<SceneManifest>();
25+
m_UIDocument = GetComponent<UIDocument>();
26+
27+
m_CameraManipulator = FindFirstObjectByType<CameraManipulator>();
28+
m_CameraManipulator.CameraSize = 15;
29+
m_CameraManipulator.CameraPosition = new Vector2(0f, 9f);
30+
31+
// Set up the scene reset action.
32+
m_SandboxManager.SceneResetAction = SetupScene;
33+
34+
// Set Overrides.
35+
m_SandboxManager.SetOverrideColorShapeState(false);
36+
37+
m_PhysicsWorld = PhysicsWorld.defaultWorld;
38+
39+
SetupOptions();
40+
41+
SetupScene();
42+
}
43+
44+
private void OnDisable()
45+
{
46+
// Reset overrides.
47+
m_SandboxManager.ResetOverrideColorShapeState();
48+
}
49+
50+
private void SetupOptions()
51+
{
52+
var root = m_UIDocument.rootVisualElement;
53+
54+
{
55+
// Menu Region (for camera manipulator).
56+
var menuRegion = root.Q<VisualElement>("menu-region");
57+
menuRegion.RegisterCallback<PointerEnterEvent>(_ => ++m_CameraManipulator.OverlapUI);
58+
menuRegion.RegisterCallback<PointerLeaveEvent>(_ => --m_CameraManipulator.OverlapUI);
59+
60+
// Intrude Shape.
61+
m_IntrudeShapeButton = root.Q<Button>("intrude-shape");
62+
m_IntrudeShapeButton.clicked += IntrudeShape;
63+
64+
// Reset Scene.
65+
var resetScene = root.Q<Button>("reset-scene");
66+
resetScene.clicked += SetupScene;
67+
68+
// Fetch the scene description.
69+
var sceneDescription = root.Q<Label>("scene-description");
70+
sceneDescription.text = $"\"{m_SceneManifest.LoadedSceneName}\"\n{m_SceneManifest.LoadedSceneDescription}";
71+
}
72+
}
73+
74+
private void SetupScene()
75+
{
76+
// Reset the scene state.
77+
m_SandboxManager.ResetSceneState();
78+
79+
var bodies = m_SandboxManager.Bodies;
80+
81+
// Ground.
82+
{
83+
var body = m_PhysicsWorld.CreateBody();
84+
bodies.Add(body);
85+
using var vertices = new NativeList<Vector2>(Allocator.Temp)
86+
{
87+
new(-25f, 0f),
88+
new(-25f, 23f),
89+
new(25f, 23f),
90+
new(25f, 0f)
91+
};
92+
body.CreateChain(new ChainGeometry(vertices.AsArray()), PhysicsChainDefinition.defaultDefinition);
93+
}
94+
95+
// Table 1.
96+
{
97+
m_Table1 = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = new Vector2(-15f, 1f) });
98+
bodies.Add(m_Table1);
99+
100+
m_Table1.CreateShape(PolygonGeometry.CreateBox(new Vector2(6f, 1f), 0f, new Vector2(0f, 3.5f)));
101+
m_Table1.CreateShape(PolygonGeometry.CreateBox(new Vector2(1f, 3f), 0f, new Vector2(-2.5f, 1.5f)));
102+
m_Table1.CreateShape(PolygonGeometry.CreateBox(new Vector2(1f, 3f), 0f, new Vector2(2.5f, 1.5f)));
103+
}
104+
105+
// Table 2.
106+
{
107+
m_Table2 = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = new Vector2(-5f, 1f) });
108+
bodies.Add(m_Table2);
109+
110+
m_Table2.CreateShape(PolygonGeometry.CreateBox(new Vector2(6f, 1f), 0f, new Vector2(0f, 3.5f)));
111+
m_Table2.CreateShape(PolygonGeometry.CreateBox(new Vector2(1f, 4f), 0f, new Vector2(-2.5f, 2f)));
112+
m_Table2.CreateShape(PolygonGeometry.CreateBox(new Vector2(1f, 4f), 0f, new Vector2(2.5f, 2f)));
113+
}
114+
115+
// Spaceship 1.
116+
{
117+
m_Ship1 = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = new Vector2(5f, 1f) });
118+
bodies.Add(m_Ship1);
119+
120+
{
121+
using var vertices = new NativeList<Vector2>(Allocator.Temp)
122+
{
123+
new(-2f, 0f),
124+
new(0f, 4f / 3f),
125+
new(0f, 4f)
126+
};
127+
m_Ship1.CreateShape(PolygonGeometry.Create(vertices.AsArray()));
128+
}
129+
130+
{
131+
using var vertices = new NativeList<Vector2>(Allocator.Temp)
132+
{
133+
new(2f, 0f),
134+
new(0f, 4f / 3f),
135+
new(0f, 4f)
136+
};
137+
m_Ship1.CreateShape(PolygonGeometry.Create(vertices.AsArray()));
138+
}
139+
}
140+
141+
// Spaceship 2.
142+
{
143+
m_Ship2 = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = new Vector2(15f, 1f) });
144+
bodies.Add(m_Ship2);
145+
146+
{
147+
using var vertices = new NativeList<Vector2>(Allocator.Temp)
148+
{
149+
new(-2f, 0f),
150+
new(1f, 2f),
151+
new(0f, 4f)
152+
};
153+
m_Ship2.CreateShape(PolygonGeometry.Create(vertices.AsArray()));
154+
}
155+
156+
{
157+
using var vertices = new NativeList<Vector2>(Allocator.Temp)
158+
{
159+
new(2f, 0f),
160+
new(-1f, 2f),
161+
new(0f, 4f)
162+
};
163+
m_Ship2.CreateShape(PolygonGeometry.Create(vertices.AsArray()));
164+
}
165+
}
166+
}
167+
168+
private void IntrudeShape()
169+
{
170+
var bodies = m_SandboxManager.Bodies;
171+
172+
// Table 1 intrusion.
173+
{
174+
var body = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = m_Table1.position, rotation = m_Table1.rotation });
175+
bodies.Add(body);
176+
body.CreateShape(PolygonGeometry.CreateBox(new Vector2(8f, 0.2f), 0f, new Vector2(0f, 3.0f)));
177+
}
178+
179+
// Table 2 intrusion.
180+
{
181+
var body = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = m_Table2.position, rotation = m_Table2.rotation });
182+
bodies.Add(body);
183+
body.CreateShape(PolygonGeometry.CreateBox(new Vector2(8f, 0.2f), 0f, new Vector2(0f, 3.0f)));
184+
}
185+
186+
// Ship 1 intrusion.
187+
{
188+
var body = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = m_Ship1.position, rotation = m_Ship1.rotation });
189+
bodies.Add(body);
190+
body.CreateShape(new CircleGeometry { center = new Vector2(0f, 2f), radius = 0.5f });
191+
}
192+
193+
// Ship 2 intrusion.
194+
{
195+
var body = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = m_Ship2.position, rotation = m_Ship2.rotation });
196+
bodies.Add(body);
197+
body.CreateShape(new CircleGeometry { center = new Vector2(0f, 2f), radius = 0.5f });
198+
}
199+
}
200+
201+
private void Update()
202+
{
203+
DrawBodyBounds(m_Table1, Color.red);
204+
DrawBodyBounds(m_Table2, Color.cyan);
205+
DrawBodyBounds(m_Ship1, Color.red);
206+
DrawBodyBounds(m_Ship2, Color.cyan);
207+
}
208+
209+
private void DrawBodyBounds(PhysicsBody body, Color color)
210+
{
211+
if (!body.isValid)
212+
return;
213+
214+
// Calculate the body AABB.
215+
var aabb = body.GetAABB();
216+
217+
// Draw the AABB.
218+
m_PhysicsWorld.DrawBox(aabb.center, aabb.extents * 2f, 0f, color);
219+
}
220+
}

LowLevel/Sandbox/Assets/Scenes/Shapes/Compound/Compound.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)