diff --git a/Source/Engine/Level/Actors/BoxCollider.cs b/Source/Engine/Level/Actors/BoxCollider.cs deleted file mode 100644 index cfb6e9dde..000000000 --- a/Source/Engine/Level/Actors/BoxCollider.cs +++ /dev/null @@ -1,56 +0,0 @@ -#if FLAX_EDITOR -using FlaxEditor.CustomEditors; -using FlaxEditor.CustomEditors.Dedicated; -#endif - -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); - } - } - - /// - /// Resizes the box collider based on the bounds of it's parent. - /// - public void AutoResize() - { - if (Parent is Scene) - { - return; - } - - LocalPosition = Vector3.Zero; - - Vector3 parentScale = Parent.Scale; - 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; - - // Undo Rotation - Orientation *= Quaternion.Invert(Orientation); - } - } -} diff --git a/Source/Engine/Physics/Colliders/BoxCollider.cpp b/Source/Engine/Physics/Colliders/BoxCollider.cpp index b80b68de0..d74b3efba 100644 --- a/Source/Engine/Physics/Colliders/BoxCollider.cpp +++ b/Source/Engine/Physics/Colliders/BoxCollider.cpp @@ -2,6 +2,7 @@ #include "BoxCollider.h" #include "Engine/Physics/PhysicsBackend.h" +#include "Engine/Level/Scene/Scene.h" BoxCollider::BoxCollider(const SpawnParams& params) : Collider(params) @@ -19,6 +20,32 @@ void BoxCollider::SetSize(const Float3& value) UpdateBounds(); } +void BoxCollider::AutoResize() +{ + Actor* parent = GetParent(); + if (Cast(parent)) + return; + + // Get bounds of all siblings (excluding itself) + const Vector3 parentScale = parent->GetScale(); + if (parentScale.IsAnyZero()) + return; // Avoid division by zero + BoundingBox parentBox = parent->GetBox(); + for (const Actor* sibling : parent->Children) + { + if (sibling != this) + BoundingBox::Merge(parentBox, sibling->GetBoxWithChildren(), parentBox); + } + const Vector3 parentSize = parentBox.GetSize(); + const Vector3 parentCenter = parentBox.GetCenter() - parent->GetPosition(); + + // Update bounds + SetLocalPosition(Vector3::Zero); + SetSize(parentSize / parentScale); + SetCenter(parentCenter / parentScale); + SetOrientation(GetOrientation() * Quaternion::Invert(GetOrientation())); +} + #if USE_EDITOR #include "Engine/Debug/DebugDraw.h" diff --git a/Source/Engine/Physics/Colliders/BoxCollider.h b/Source/Engine/Physics/Colliders/BoxCollider.h index 5bcc21b45..3c15ce640 100644 --- a/Source/Engine/Physics/Colliders/BoxCollider.h +++ b/Source/Engine/Physics/Colliders/BoxCollider.h @@ -43,6 +43,11 @@ public: return _bounds; } + /// + /// Resizes the collider based on the bounds of it's parent to contain it whole (including any siblings). + /// + API_FUNCTION() void AutoResize(); + public: // [Collider] #if USE_EDITOR