From 4e54e945efb0898ba20ff9a0de61f4f9593cb036 Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Sat, 9 Dec 2023 17:43:06 -0500
Subject: [PATCH 1/6] Implement auto-sizing for box colliders when they are
added to the scene.
---
Source/Editor/Modules/SceneModule.cs | 2 ++
Source/Engine/Level/Actor.cs | 9 ++++++++
Source/Engine/Level/Actors/BoxCollider.cs | 27 +++++++++++++++++++++++
3 files changed, 38 insertions(+)
create mode 100644 Source/Engine/Level/Actors/BoxCollider.cs
diff --git a/Source/Editor/Modules/SceneModule.cs b/Source/Editor/Modules/SceneModule.cs
index 954e3bd50..f5471ec33 100644
--- a/Source/Editor/Modules/SceneModule.cs
+++ b/Source/Editor/Modules/SceneModule.cs
@@ -534,6 +534,8 @@ namespace FlaxEditor.Modules
{
node.ParentNode = parentNode;
}
+
+ actor.OnActorSpawned();
}
private void OnActorDeleted(Actor actor)
diff --git a/Source/Engine/Level/Actor.cs b/Source/Engine/Level/Actor.cs
index dbe8a89b5..7c34b5d7e 100644
--- a/Source/Engine/Level/Actor.cs
+++ b/Source/Engine/Level/Actor.cs
@@ -116,6 +116,15 @@ namespace FlaxEngine
LocalTransform = Transform.Identity;
}
+ ///
+ /// Called in-editor when an actor is added to the scene.
+ /// If not in the editor, this function will not be called.
+ ///
+ public virtual void OnActorSpawned()
+ {
+
+ }
+
///
/// Creates a new child actor of the given type.
///
diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs
new file mode 100644
index 000000000..f9343eb74
--- /dev/null
+++ b/Source/Engine/Level/Actors/BoxCollider.cs
@@ -0,0 +1,27 @@
+using System;
+
+namespace FlaxEngine
+{
+ partial class BoxCollider
+ {
+ ///
+ public override void OnActorSpawned()
+ {
+ base.OnActorSpawned();
+ if (Parent is StaticModel model)
+ {
+ Vector3 modelScale = model.Scale;
+ Vector3 modelSize = model.Box.Size;
+ Vector3 modelCenter = model.Box.Center - model.Position;
+ Vector3 colliderSize = modelSize / modelScale;
+ Vector3 colliderCenter = modelCenter / modelScale;
+
+ Size = colliderSize;
+ Center = colliderCenter;
+
+ // Undo Rotation
+ Orientation *= Quaternion.Invert(Orientation);
+ }
+ }
+ }
+}
From 2f500425239d206f9afd0afa43656999d9e35027 Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Sat, 9 Dec 2023 17:56:09 -0500
Subject: [PATCH 2/6] Simplify code and allow any actor as parent.
---
Source/Engine/Level/Actors/BoxCollider.cs | 19 +++++++------------
1 file changed, 7 insertions(+), 12 deletions(-)
diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs
index f9343eb74..8f2d0e366 100644
--- a/Source/Engine/Level/Actors/BoxCollider.cs
+++ b/Source/Engine/Level/Actors/BoxCollider.cs
@@ -8,20 +8,15 @@ namespace FlaxEngine
public override void OnActorSpawned()
{
base.OnActorSpawned();
- if (Parent is StaticModel model)
- {
- Vector3 modelScale = model.Scale;
- Vector3 modelSize = model.Box.Size;
- Vector3 modelCenter = model.Box.Center - model.Position;
- Vector3 colliderSize = modelSize / modelScale;
- Vector3 colliderCenter = modelCenter / modelScale;
+ Vector3 parentScale = Parent.Scale;
+ Vector3 parentSize = Parent.Box.Size;
+ Vector3 parentCenter = Parent.Box.Center - Parent.Position;
- Size = colliderSize;
- Center = colliderCenter;
+ Size = parentSize / parentScale;
+ Center = parentCenter / parentScale;
- // Undo Rotation
- Orientation *= Quaternion.Invert(Orientation);
- }
+ // Undo Rotation
+ Orientation *= Quaternion.Invert(Orientation);
}
}
}
From c895e310cbec7a6fbca418ff419cd64e08b13cb4 Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Sat, 9 Dec 2023 19:19:03 -0500
Subject: [PATCH 3/6] Improve box collider creation behavior to account for
child actors of the collider's parent.
---
Source/Engine/Level/Actors/BoxCollider.cs | 29 ++++++++++++++++++++---
1 file changed, 26 insertions(+), 3 deletions(-)
diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs
index 8f2d0e366..a21f30aeb 100644
--- a/Source/Engine/Level/Actors/BoxCollider.cs
+++ b/Source/Engine/Level/Actors/BoxCollider.cs
@@ -1,16 +1,39 @@
-using System;
+using FlaxEditor.Windows;
namespace FlaxEngine
{
partial class BoxCollider
{
+ private void BoxExcluding(Actor target, ref BoundingBox output, Actor excluded)
+ {
+ foreach (Actor child in target.Children)
+ {
+ if (child == excluded)
+ {
+ continue;
+ }
+
+ output = BoundingBox.Merge(output, child.Box);
+ BoxExcluding(child, ref output, excluded);
+ }
+ }
+
///
public override void OnActorSpawned()
{
base.OnActorSpawned();
Vector3 parentScale = Parent.Scale;
- Vector3 parentSize = Parent.Box.Size;
- Vector3 parentCenter = Parent.Box.Center - Parent.Position;
+ BoundingBox parentBox = Parent.Box;
+ BoxExcluding(Parent, ref parentBox, this);
+
+ Vector3 parentSize = parentBox.Size;
+ Vector3 parentCenter = parentBox.Center - Parent.Position;
+
+ // Avoid division by zero
+ if (parentScale.X == 0 || parentScale.Y == 0 || parentScale.Z == 0)
+ {
+ return;
+ }
Size = parentSize / parentScale;
Center = parentCenter / parentScale;
From 01b233af109171b1ce8b9e317ded04d869e5fe77 Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Sat, 9 Dec 2023 19:57:52 -0500
Subject: [PATCH 4/6] Factor out AutoResize() function for external use. Fix
build regression due to unwanted import.
---
Source/Engine/Level/Actors/BoxCollider.cs | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs
index a21f30aeb..9c816eac4 100644
--- a/Source/Engine/Level/Actors/BoxCollider.cs
+++ b/Source/Engine/Level/Actors/BoxCollider.cs
@@ -1,5 +1,3 @@
-using FlaxEditor.Windows;
-
namespace FlaxEngine
{
partial class BoxCollider
@@ -18,10 +16,16 @@ namespace FlaxEngine
}
}
- ///
- public override void OnActorSpawned()
+ ///
+ /// Resizes the box collider based on the bounds of it's parent.
+ ///
+ public void AutoResize()
{
- base.OnActorSpawned();
+ if (Parent is Scene)
+ {
+ return;
+ }
+
Vector3 parentScale = Parent.Scale;
BoundingBox parentBox = Parent.Box;
BoxExcluding(Parent, ref parentBox, this);
@@ -41,5 +45,12 @@ namespace FlaxEngine
// Undo Rotation
Orientation *= Quaternion.Invert(Orientation);
}
+
+ ///
+ public override void OnActorSpawned()
+ {
+ base.OnActorSpawned();
+ AutoResize();
+ }
}
}
From 19dbd3c4e4f6c562adc9dde920dca5b825e7a264 Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Sat, 9 Dec 2023 21:00:45 -0500
Subject: [PATCH 5/6] Add button to resize collider manually.
---
Source/Engine/Level/Actors/BoxCollider.cs | 27 +++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs
index 9c816eac4..ff560aa64 100644
--- a/Source/Engine/Level/Actors/BoxCollider.cs
+++ b/Source/Engine/Level/Actors/BoxCollider.cs
@@ -1,5 +1,30 @@
+#if FLAX_EDITOR
+using FlaxEditor.CustomEditors;
+using FlaxEditor.CustomEditors.Dedicated;
+#endif
+
namespace FlaxEngine
{
+#if FLAX_EDITOR
+ ///
+ /// Dedicated custom editor for BoxCollider objects.
+ ///
+ [CustomEditor(typeof(BoxCollider)), DefaultEditor]
+ public class BoxColliderEditor : ActorEditor
+ {
+ ///
+ public override void Initialize(LayoutElementsContainer layout)
+ {
+ base.Initialize(layout);
+ layout.Space(20f);
+ var autoResizeButton = layout.Button("Resize to Fit", "Resize the box collider to fit it's parent's bounds.");
+
+ BoxCollider collider = Values[0] as BoxCollider;
+ autoResizeButton.Button.Clicked += collider.AutoResize;
+ }
+ }
+#endif
+
partial class BoxCollider
{
private void BoxExcluding(Actor target, ref BoundingBox output, Actor excluded)
@@ -26,6 +51,8 @@ namespace FlaxEngine
return;
}
+ LocalPosition = Vector3.Zero;
+
Vector3 parentScale = Parent.Scale;
BoundingBox parentBox = Parent.Box;
BoxExcluding(Parent, ref parentBox, this);
From 945438568341cf16988910d62e5103652d8cd266 Mon Sep 17 00:00:00 2001
From: Menotdan <32620310+Menotdan@users.noreply.github.com>
Date: Tue, 12 Dec 2023 15:46:15 -0500
Subject: [PATCH 6/6] Use the BoxColliderNode class for handling actor spawn
events.
---
Source/Editor/Modules/SceneModule.cs | 2 --
.../SceneGraph/Actors/BoxColliderNode.cs | 33 +++++++++++++++++++
Source/Engine/Level/Actor.cs | 9 -----
Source/Engine/Level/Actors/BoxCollider.cs | 27 ---------------
4 files changed, 33 insertions(+), 38 deletions(-)
diff --git a/Source/Editor/Modules/SceneModule.cs b/Source/Editor/Modules/SceneModule.cs
index f5471ec33..954e3bd50 100644
--- a/Source/Editor/Modules/SceneModule.cs
+++ b/Source/Editor/Modules/SceneModule.cs
@@ -534,8 +534,6 @@ namespace FlaxEditor.Modules
{
node.ParentNode = parentNode;
}
-
- actor.OnActorSpawned();
}
private void OnActorDeleted(Actor actor)
diff --git a/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs b/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs
index 1f92eceea..8c00318db 100644
--- a/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs
+++ b/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs
@@ -8,8 +8,33 @@ using Real = System.Single;
using FlaxEngine;
+#if FLAX_EDITOR
+using FlaxEditor.CustomEditors.Dedicated;
+using FlaxEditor.CustomEditors;
+#endif
+
namespace FlaxEditor.SceneGraph.Actors
{
+#if FLAX_EDITOR
+ ///
+ /// Dedicated custom editor for BoxCollider objects.
+ ///
+ [CustomEditor(typeof(BoxCollider)), DefaultEditor]
+ public class BoxColliderEditor : ActorEditor
+ {
+ ///
+ public override void Initialize(LayoutElementsContainer layout)
+ {
+ base.Initialize(layout);
+ layout.Space(20f);
+ var autoResizeButton = layout.Button("Resize to Fit", "Resize the box collider to fit it's parent's bounds.");
+
+ BoxCollider collider = Values[0] as BoxCollider;
+ autoResizeButton.Button.Clicked += collider.AutoResize;
+ }
+ }
+#endif
+
///
/// Scene tree node for actor type.
///
@@ -37,5 +62,13 @@ namespace FlaxEditor.SceneGraph.Actors
return base.RayCastSelf(ref ray, out distance, out normal);
}
+
+ ///
+ public override void PostSpawn()
+ {
+ base.PostSpawn();
+ BoxCollider boxCollider = Actor as BoxCollider;
+ boxCollider.AutoResize();
+ }
}
}
diff --git a/Source/Engine/Level/Actor.cs b/Source/Engine/Level/Actor.cs
index 7c34b5d7e..dbe8a89b5 100644
--- a/Source/Engine/Level/Actor.cs
+++ b/Source/Engine/Level/Actor.cs
@@ -116,15 +116,6 @@ namespace FlaxEngine
LocalTransform = Transform.Identity;
}
- ///
- /// Called in-editor when an actor is added to the scene.
- /// If not in the editor, this function will not be called.
- ///
- public virtual void OnActorSpawned()
- {
-
- }
-
///
/// Creates a new child actor of the given type.
///
diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs
index ff560aa64..cfb6e9dde 100644
--- a/Source/Engine/Level/Actors/BoxCollider.cs
+++ b/Source/Engine/Level/Actors/BoxCollider.cs
@@ -5,26 +5,6 @@ using FlaxEditor.CustomEditors.Dedicated;
namespace FlaxEngine
{
-#if FLAX_EDITOR
- ///
- /// Dedicated custom editor for BoxCollider objects.
- ///
- [CustomEditor(typeof(BoxCollider)), DefaultEditor]
- public class BoxColliderEditor : ActorEditor
- {
- ///
- public override void Initialize(LayoutElementsContainer layout)
- {
- base.Initialize(layout);
- layout.Space(20f);
- var autoResizeButton = layout.Button("Resize to Fit", "Resize the box collider to fit it's parent's bounds.");
-
- BoxCollider collider = Values[0] as BoxCollider;
- autoResizeButton.Button.Clicked += collider.AutoResize;
- }
- }
-#endif
-
partial class BoxCollider
{
private void BoxExcluding(Actor target, ref BoundingBox output, Actor excluded)
@@ -72,12 +52,5 @@ namespace FlaxEngine
// Undo Rotation
Orientation *= Quaternion.Invert(Orientation);
}
-
- ///
- public override void OnActorSpawned()
- {
- base.OnActorSpawned();
- AutoResize();
- }
}
}