From 7a63d33f92e2e98c6d5c6fffd718d499ce848669 Mon Sep 17 00:00:00 2001 From: greerdv Date: Fri, 11 Feb 2022 12:01:22 +0000 Subject: [PATCH] extract reused manipulator scaling logic Signed-off-by: greerdv --- .../ComponentModes/BoxViewportEdit.cpp | 27 +++++++++---------- .../ComponentModes/BoxViewportEdit.h | 6 +++++ .../PhysX/Code/Editor/ColliderCapsuleMode.cpp | 19 +++++-------- Gems/PhysX/Code/Editor/ColliderSphereMode.cpp | 10 +++---- 4 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.cpp b/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.cpp index cf3d8aa693..a747bb1379 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.cpp +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.cpp @@ -93,29 +93,20 @@ namespace AzToolsFramework linearManipulator->SetViews(AZStd::move(views)); linearManipulator->InstallMouseMoveCallback( - [this, entityComponentIdPair]( - const LinearManipulator::Action& action) + [this, entityComponentIdPair, + transformScale{ linearManipulator->GetSpace().GetUniformScale() }](const LinearManipulator::Action& action) { AZ::Transform boxLocalTransform = AZ::Transform::CreateIdentity(); BoxManipulatorRequestBus::EventResult( boxLocalTransform, entityComponentIdPair, &BoxManipulatorRequests::GetCurrentLocalTransform); - AZ::Transform boxWorldTransform = AZ::Transform::CreateIdentity(); - AZ::TransformBus::EventResult( - boxWorldTransform, m_entityComponentIdPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldTM); - float boxScale = AZ::GetMax(AZ::MinTransformScale, boxWorldTransform.GetUniformScale()); - - // calculate the position of the manipulator in the reference frame of the box - // the local position offset of the manipulator does not take the transform scale into account - // so need to apply it here - const AZ::Vector3 localPosition = boxLocalTransform.GetInverse().TransformPoint( - action.m_start.m_localPosition + action.m_current.m_localPositionOffset / boxScale); + const AZ::Vector3 manipulatorPosition = GetPositionInManipulatorFrame(transformScale, boxLocalTransform, action); // calculate the amount of displacement along an axis this manipulator has moved // clamp movement so it cannot go negative based on axis direction const AZ::Vector3 axisDisplacement = - localPosition.GetAbs() * 2.0f - * AZ::GetMax(0.0f, localPosition.GetNormalized().Dot(action.m_fixed.m_axis)); + manipulatorPosition.GetAbs() * 2.0f + * AZ::GetMax(0.0f, manipulatorPosition.GetNormalized().Dot(action.m_fixed.m_axis)); AZ::Vector3 boxDimensions = AZ::Vector3::CreateZero(); BoxManipulatorRequestBus::EventResult( @@ -148,4 +139,12 @@ namespace AzToolsFramework } } } + + AZ::Vector3 GetPositionInManipulatorFrame(float worldUniformScale, const AZ::Transform& manipulatorLocalTransform, + const LinearManipulator::Action& action) + { + return manipulatorLocalTransform.GetInverse().TransformPoint( + action.m_start.m_localPosition + + action.m_current.m_localPositionOffset / AZ::GetClamp(worldUniformScale, AZ::MinTransformScale, AZ::MaxTransformScale)); + } } // namespace AzToolsFramework diff --git a/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.h b/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.h index 98ef17e547..66ea626e12 100644 --- a/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.h +++ b/Code/Framework/AzToolsFramework/AzToolsFramework/ComponentModes/BoxViewportEdit.h @@ -31,4 +31,10 @@ namespace AzToolsFramework using BoxManipulators = AZStd::array, 6>; BoxManipulators m_linearManipulators; ///< Manipulators for editing box size. }; + + /// Calculates the position of the manipulator in its own reference frame. + /// Removes the effects of the manipulator local transform, and accounts for world transform scale in + /// the action local offset. + AZ::Vector3 GetPositionInManipulatorFrame(float worldUniformScale, const AZ::Transform& manipulatorLocalTransform, + const LinearManipulator::Action& action); } // namespace AzToolsFramework diff --git a/Gems/PhysX/Code/Editor/ColliderCapsuleMode.cpp b/Gems/PhysX/Code/Editor/ColliderCapsuleMode.cpp index eddd65a7f0..b704f8feec 100644 --- a/Gems/PhysX/Code/Editor/ColliderCapsuleMode.cpp +++ b/Gems/PhysX/Code/Editor/ColliderCapsuleMode.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -175,15 +176,12 @@ namespace PhysX void ColliderCapsuleMode::OnRadiusManipulatorMoved(const AzToolsFramework::LinearManipulator::Action& action, const AZ::EntityComponentIdPair& idPair) { // manipulator action offsets do not take entity transform scale into account, so need to apply it here - float transformScale = 1.0f; const AZ::Transform colliderLocalTransform = Utils::GetColliderLocalTransform(idPair); - AZ::TransformBus::EventResult(transformScale, idPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldUniformScale); - transformScale = AZ::GetMax(AZ::MinTransformScale, transformScale); - const AZ::Vector3 localPosition = colliderLocalTransform.GetInverse().TransformPoint( - action.m_start.m_localPosition + action.m_current.m_localPositionOffset / transformScale); + const AZ::Vector3 manipulatorPosition = AzToolsFramework::GetPositionInManipulatorFrame( + m_radiusManipulator->GetSpace().GetUniformScale(), colliderLocalTransform, action); // Get the distance the manipulator has moved along the axis. - float extent = localPosition.Dot(action.m_fixed.m_axis); + float extent = manipulatorPosition.Dot(action.m_fixed.m_axis); // Clamp radius to a small value. extent = AZ::GetMax(extent, MinCapsuleRadius); @@ -201,15 +199,12 @@ namespace PhysX void ColliderCapsuleMode::OnHeightManipulatorMoved(const AzToolsFramework::LinearManipulator::Action& action, const AZ::EntityComponentIdPair& idPair) { // manipulator action offsets do not take entity transform scale into account, so need to apply it here - float transformScale = 1.0f; const AZ::Transform colliderLocalTransform = Utils::GetColliderLocalTransform(idPair); - AZ::TransformBus::EventResult(transformScale, idPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldUniformScale); - transformScale = AZ::GetMax(AZ::MinTransformScale, transformScale); - const AZ::Vector3 localPosition = colliderLocalTransform.GetInverse().TransformPoint( - action.m_start.m_localPosition + action.m_current.m_localPositionOffset / transformScale); + const AZ::Vector3 manipulatorPosition = AzToolsFramework::GetPositionInManipulatorFrame( + m_heightManipulator->GetSpace().GetUniformScale(), colliderLocalTransform, action); // Get the distance the manipulator has moved along the axis. - float extent = localPosition.Dot(action.m_fixed.m_axis); + float extent = manipulatorPosition.Dot(action.m_fixed.m_axis); // Ensure capsule's half height is always greater than the radius. extent = AZ::GetMax(extent, MinCapsuleHeight); diff --git a/Gems/PhysX/Code/Editor/ColliderSphereMode.cpp b/Gems/PhysX/Code/Editor/ColliderSphereMode.cpp index d292f2dd2a..8d69e28726 100644 --- a/Gems/PhysX/Code/Editor/ColliderSphereMode.cpp +++ b/Gems/PhysX/Code/Editor/ColliderSphereMode.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -110,15 +111,12 @@ namespace PhysX void ColliderSphereMode::OnManipulatorMoved(const AzToolsFramework::LinearManipulator::Action& action, const AZ::EntityComponentIdPair& idPair) { // manipulator offsets do not take transform scale into account, need to handle it here - AZ::Transform colliderWorldTransform = AZ::Transform::CreateIdentity(); - AZ::TransformBus::EventResult(colliderWorldTransform, idPair.GetEntityId(), &AZ::TransformBus::Events::GetWorldTM); const AZ::Transform colliderLocalTransform = Utils::GetColliderLocalTransform(idPair); - const float transformScale = AZ::GetMax(AZ::MinTransformScale, colliderWorldTransform.GetUniformScale()); - const AZ::Vector3 localPosition = colliderLocalTransform.GetInverse().TransformPoint( - action.m_start.m_localPosition + action.m_current.m_localPositionOffset / transformScale); + const AZ::Vector3 manipulatorPosition = AzToolsFramework::GetPositionInManipulatorFrame( + m_radiusManipulator->GetSpace().GetUniformScale(), colliderLocalTransform, action); // Get the distance the manipulator has moved along the axis. - float extent = localPosition.Dot(action.m_fixed.m_axis); + float extent = manipulatorPosition.Dot(action.m_fixed.m_axis); // Clamp the distance to a small value to prevent it going negative. extent = AZ::GetMax(extent, MinSphereRadius);