add logic to convert matrix3x3 to euler angles
This commit is contained in:
@@ -24,6 +24,48 @@ AZ_CVAR(
|
||||
|
||||
namespace AzFramework
|
||||
{
|
||||
// Based on paper by David Eberly - https://www.geometrictools.com/Documentation/EulerAngles.pdf
|
||||
AZ::Vector3 EulerAngles(const AZ::Matrix3x3& orientation)
|
||||
{
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
|
||||
// 2.4 Factor as RzRyRx
|
||||
if (orientation.GetElement(2, 0) < 1.0f)
|
||||
{
|
||||
if (orientation.GetElement(2, 0) > -1.0f)
|
||||
{
|
||||
x = std::atan2(orientation.GetElement(2, 1), orientation.GetElement(2, 2));
|
||||
y = std::asin(-orientation.GetElement(2, 0));
|
||||
z = std::atan2(orientation.GetElement(1, 0), orientation.GetElement(0, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
x = 0.0f;
|
||||
y = AZ::Constants::Pi * 0.5f;
|
||||
z = -std::atan2(-orientation.GetElement(2, 1), orientation.GetElement(1, 1));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
x = 0.0f;
|
||||
y = -AZ::Constants::Pi * 0.5f;
|
||||
z = std::atan2(-orientation.GetElement(1, 2), orientation.GetElement(1, 1));
|
||||
}
|
||||
|
||||
return {x, y, z};
|
||||
}
|
||||
|
||||
void UpdateCameraFromTransform(Camera& camera, const AZ::Transform& transform)
|
||||
{
|
||||
const auto eulerAngles = AzFramework::EulerAngles(AZ::Matrix3x3::CreateFromTransform(transform));
|
||||
|
||||
camera.m_lookAt = transform.GetTranslation();
|
||||
camera.m_pitch = eulerAngles.GetX();
|
||||
camera.m_yaw = eulerAngles.GetZ();
|
||||
}
|
||||
|
||||
bool CameraSystem::HandleEvents(const InputEvent& event)
|
||||
{
|
||||
if (const auto& cursor_motion = AZStd::get_if<CursorMotionEvent>(&event))
|
||||
|
||||
@@ -43,6 +43,9 @@ namespace AzFramework
|
||||
|
||||
using ModernViewportCameraControllerRequestBus = AZ::EBus<ModernViewportCameraControllerRequests>;
|
||||
|
||||
// https://www.geometrictools.com/Documentation/EulerAngles.pdf
|
||||
AZ::Vector3 EulerAngles(const AZ::Matrix3x3& orientation);
|
||||
|
||||
struct Camera
|
||||
{
|
||||
AZ::Vector3 m_lookAt = AZ::Vector3::CreateZero(); //!< Position of camera when m_lookDist is zero,
|
||||
@@ -83,6 +86,8 @@ namespace AzFramework
|
||||
return Transform().GetTranslation();
|
||||
}
|
||||
|
||||
void UpdateCameraFromTransform(Camera& camera, const AZ::Transform& transform);
|
||||
|
||||
struct CursorMotionEvent
|
||||
{
|
||||
ScreenPoint m_position;
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
#include <AzFramework/Windowing/WindowBus.h>
|
||||
#include <AzToolsFramework/Viewport/ViewportMessages.h>
|
||||
|
||||
AZ_CVAR(bool, ed_newCameraSystemDebug, false, nullptr, AZ::ConsoleFunctorFlags::Null, "Enable debug drawing for the new camera system");
|
||||
|
||||
namespace SandboxEditor
|
||||
{
|
||||
static AZ::RPI::ViewportContextPtr RetrieveViewportContext(const AzFramework::ViewportId viewportId)
|
||||
@@ -68,16 +66,6 @@ namespace SandboxEditor
|
||||
m_cameraSystem.m_cameras.AddCamera(firstPersonWheelCamera);
|
||||
m_cameraSystem.m_cameras.AddCamera(orbitCamera);
|
||||
|
||||
if (const auto viewportContext = RetrieveViewportContext(viewportId))
|
||||
{
|
||||
// set position but not orientation
|
||||
m_targetCamera.m_lookAt = viewportContext->GetCameraTransform().GetTranslation();
|
||||
|
||||
// LYN-2315 TODO https://www.geometrictools.com/Documentation/EulerAngles.pdf
|
||||
|
||||
m_camera = m_targetCamera;
|
||||
}
|
||||
|
||||
AzFramework::ViewportDebugDisplayEventBus::Handler::BusConnect(AzToolsFramework::GetEntityContextId());
|
||||
AzFramework::ModernViewportCameraControllerRequestBus::Handler::BusConnect(viewportId);
|
||||
}
|
||||
@@ -111,15 +99,12 @@ namespace SandboxEditor
|
||||
void ModernViewportCameraControllerInstance::DisplayViewport(
|
||||
[[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo, AzFramework::DebugDisplayRequests& debugDisplay)
|
||||
{
|
||||
if (ed_newCameraSystemDebug)
|
||||
{
|
||||
debugDisplay.SetColor(AZ::Colors::White);
|
||||
debugDisplay.DrawWireSphere(m_targetCamera.m_lookAt, 0.5f);
|
||||
}
|
||||
debugDisplay.SetColor(1.0f, 1.0f, 1.0f, AZStd::min(-m_camera.m_lookDist / 5.0f, 1.0f));
|
||||
debugDisplay.DrawWireSphere(m_camera.m_lookAt, 0.5f);
|
||||
}
|
||||
|
||||
void ModernViewportCameraControllerInstance::SetTargetCameraTransform(const AZ::Transform& transform)
|
||||
{
|
||||
m_targetCamera.m_lookAt = transform.GetTranslation();
|
||||
AzFramework::UpdateCameraFromTransform(m_targetCamera, transform);
|
||||
}
|
||||
} // namespace SandboxEditor
|
||||
|
||||
Reference in New Issue
Block a user