Merge branch 'coll-resize-orient' of https://github.com/Tryibion/FlaxEngine into Tryibion-coll-resize-orient

This commit is contained in:
Wojtek Figat
2024-02-20 11:07:12 +01:00
3 changed files with 19 additions and 5 deletions

View File

@@ -19,12 +19,17 @@ namespace FlaxEditor.SceneGraph.Actors
[CustomEditor(typeof(BoxCollider)), DefaultEditor]
public class BoxColliderEditor : ActorEditor
{
private bool _keepLocalOrientation = true;
/// <inheritdoc />
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);
}
}
}

View File

@@ -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<Scene>(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);
if (globalOrientation)
SetOrientation(GetOrientation() * Quaternion::Invert(GetOrientation()));
else
SetOrientation(parentOrientation);
}
#if USE_EDITOR

View File

@@ -46,7 +46,7 @@ public:
/// <summary>
/// Resizes the collider based on the bounds of it's parent to contain it whole (including any siblings).
/// </summary>
API_FUNCTION() void AutoResize();
API_FUNCTION() void AutoResize(bool globalOrientation);
public:
// [Collider]