diff --git a/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs b/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs index 0f6670feb..64ab104c5 100644 --- a/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs +++ b/Source/Editor/SceneGraph/Actors/BoxColliderNode.cs @@ -19,12 +19,17 @@ namespace FlaxEditor.SceneGraph.Actors [CustomEditor(typeof(BoxCollider)), DefaultEditor] public class BoxColliderEditor : ActorEditor { + private bool _keepLocalOrientation = true; + /// public override void Initialize(LayoutElementsContainer layout) { base.Initialize(layout); layout.Space(20f); + var checkbox = layout.Checkbox("Keep Local Orientation", "Keeps the local orientation when resizing.").CheckBox; + checkbox.Checked = _keepLocalOrientation; + checkbox.StateChanged += box => _keepLocalOrientation = box.Checked; layout.Button("Resize to Fit", Editor.Instance.CodeDocs.GetTooltip(new ScriptMemberInfo(typeof(BoxCollider).GetMethod("AutoResize")))).Button.Clicked += OnResizeClicked; } @@ -33,7 +38,7 @@ namespace FlaxEditor.SceneGraph.Actors foreach (var value in Values) { if (value is BoxCollider collider) - collider.AutoResize(); + collider.AutoResize(!_keepLocalOrientation); } } } @@ -76,7 +81,7 @@ namespace FlaxEditor.SceneGraph.Actors return; } - ((BoxCollider)Actor).AutoResize(); + ((BoxCollider)Actor).AutoResize(false); } } } diff --git a/Source/Engine/Physics/Colliders/BoxCollider.cpp b/Source/Engine/Physics/Colliders/BoxCollider.cpp index d74b3efba..a174b2c11 100644 --- a/Source/Engine/Physics/Colliders/BoxCollider.cpp +++ b/Source/Engine/Physics/Colliders/BoxCollider.cpp @@ -20,7 +20,7 @@ void BoxCollider::SetSize(const Float3& value) UpdateBounds(); } -void BoxCollider::AutoResize() +void BoxCollider::AutoResize(bool globalOrientation = true) { Actor* parent = GetParent(); if (Cast(parent)) @@ -30,7 +30,13 @@ void BoxCollider::AutoResize() const Vector3 parentScale = parent->GetScale(); if (parentScale.IsAnyZero()) return; // Avoid division by zero + + // Hacky way to get unrotated bounded box of parent. + const Quaternion parentOrientation = parent->GetOrientation(); + parent->SetOrientation(Quaternion::Identity); BoundingBox parentBox = parent->GetBox(); + parent->SetOrientation(parentOrientation); + for (const Actor* sibling : parent->Children) { if (sibling != this) @@ -43,7 +49,10 @@ void BoxCollider::AutoResize() SetLocalPosition(Vector3::Zero); SetSize(parentSize / parentScale); SetCenter(parentCenter / parentScale); - SetOrientation(GetOrientation() * Quaternion::Invert(GetOrientation())); + if (globalOrientation) + SetOrientation(GetOrientation() * Quaternion::Invert(GetOrientation())); + else + SetOrientation(parentOrientation); } #if USE_EDITOR diff --git a/Source/Engine/Physics/Colliders/BoxCollider.h b/Source/Engine/Physics/Colliders/BoxCollider.h index 3c15ce640..3c6759ee9 100644 --- a/Source/Engine/Physics/Colliders/BoxCollider.h +++ b/Source/Engine/Physics/Colliders/BoxCollider.h @@ -46,7 +46,7 @@ public: /// /// Resizes the collider based on the bounds of it's parent to contain it whole (including any siblings). /// - API_FUNCTION() void AutoResize(); + API_FUNCTION() void AutoResize(bool globalOrientation); public: // [Collider]