camera move only when right-click is pressed

This commit is contained in:
SweetId 2023-11-20 15:34:41 +05:30
parent 752cd6218a
commit e8504ea3b9
1 changed files with 37 additions and 25 deletions

View File

@ -44,22 +44,30 @@ namespace
namespace Nz
{
EditorCameraComponent::EditorCameraComponent()
: m_moveSpeed(3.f)
: m_targetAngles(Nz::EulerAnglesf::Zero())
, m_targetPosition(Nz::Vector3f::Zero())
, m_currentVelocity(Nz::Vector3f::Zero())
, m_moveSpeed(3.f)
, m_smoothSpeed(0.3f)
{
auto& handler = EditorBaseApplication::Instance()->GetWindow()->GetEventHandler();
m_onMouseMoved.Connect(handler.OnMouseMoved, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
{
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.3f; // Sensibilité de la souris
if (Nz::Mouse::IsButtonPressed(Nz::Mouse::Button::Right))
{
// Gestion de la caméra free-fly (Rotation)
float sensitivity = 0.1f; // Sensibilité de la souris
// On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris
m_targetAngles.yaw = m_targetAngles.yaw - event.deltaX * sensitivity;
m_targetAngles.yaw.Normalize();
auto delta = Nz::Vector2i{ event.deltaX, event.deltaY };
// Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles
m_targetAngles.pitch = Nz::Clamp(m_targetAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f);
// On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris
m_targetAngles.yaw = m_targetAngles.yaw - delta.x * sensitivity;
m_targetAngles.yaw.Normalize();
// Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles
m_targetAngles.pitch = Nz::Clamp(m_targetAngles.pitch - delta.y * sensitivity, -89.f, 89.f);
}
});
}
@ -70,25 +78,29 @@ namespace Nz
void EditorCameraComponent::Update(Time elapsedTime, NodeComponent& node)
{
// todo use action mapping
Nz::Vector3f delta;
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::W))
delta += Nz::Vector3f::Forward();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S))
delta += Nz::Vector3f::Backward();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::A))
delta += Nz::Vector3f::Left();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
delta += Nz::Vector3f::Right();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q))
delta += Nz::Vector3f::Up();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::E))
delta += Nz::Vector3f::Down();
auto currentRotation = m_targetAngles.ToQuaternion();
m_targetPosition += (currentRotation * delta) * elapsedTime.AsSeconds() * m_moveSpeed;
auto currentPosition = m_targetPosition;
// todo use action mapping
if (Nz::Mouse::IsButtonPressed(Nz::Mouse::Button::Right))
{
Nz::Vector3f delta = Nz::Vector3f::Zero();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::W))
delta += Nz::Vector3f::Forward();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::S))
delta += Nz::Vector3f::Backward();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::A))
delta += Nz::Vector3f::Left();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::D))
delta += Nz::Vector3f::Right();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::Q))
delta += Nz::Vector3f::Up();
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::VKey::E))
delta += Nz::Vector3f::Down();
m_targetPosition += (currentRotation * delta) * elapsedTime.AsSeconds() * m_moveSpeed;
currentPosition = SmoothDamp(node.GetPosition(), m_targetPosition, m_currentVelocity, m_smoothSpeed, elapsedTime);
}
auto currentPosition = SmoothDamp(node.GetPosition(), m_targetPosition, m_currentVelocity, m_smoothSpeed, elapsedTime);
node.SetPosition(currentPosition);
node.SetRotation(currentRotation);
}