Move managed code into native impl for #2063

This commit is contained in:
Wojtek Figat
2023-12-14 11:03:58 +01:00
parent 8a5a7851cf
commit b87a7d16fb
3 changed files with 32 additions and 56 deletions

View File

@@ -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);
}
}
/// <summary>
/// Resizes the box collider based on the bounds of it's parent.
/// </summary>
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);
}
}
}

View File

@@ -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<Scene>(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"

View File

@@ -43,6 +43,11 @@ public:
return _bounds;
}
/// <summary>
/// Resizes the collider based on the bounds of it's parent to contain it whole (including any siblings).
/// </summary>
API_FUNCTION() void AutoResize();
public:
// [Collider]
#if USE_EDITOR