camera move only when right-click is pressed
This commit is contained in:
parent
752cd6218a
commit
e8504ea3b9
|
|
@ -44,22 +44,30 @@ namespace
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
EditorCameraComponent::EditorCameraComponent()
|
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)
|
, m_smoothSpeed(0.3f)
|
||||||
{
|
{
|
||||||
auto& handler = EditorBaseApplication::Instance()->GetWindow()->GetEventHandler();
|
auto& handler = EditorBaseApplication::Instance()->GetWindow()->GetEventHandler();
|
||||||
|
|
||||||
m_onMouseMoved.Connect(handler.OnMouseMoved, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
|
m_onMouseMoved.Connect(handler.OnMouseMoved, [&](const Nz::WindowEventHandler*, const Nz::WindowEvent::MouseMoveEvent& event)
|
||||||
{
|
{
|
||||||
// Gestion de la caméra free-fly (Rotation)
|
if (Nz::Mouse::IsButtonPressed(Nz::Mouse::Button::Right))
|
||||||
float sensitivity = 0.3f; // Sensibilité de la souris
|
{
|
||||||
|
// 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
|
auto delta = Nz::Vector2i{ event.deltaX, event.deltaY };
|
||||||
m_targetAngles.yaw = m_targetAngles.yaw - event.deltaX * sensitivity;
|
|
||||||
m_targetAngles.yaw.Normalize();
|
|
||||||
|
|
||||||
// Idem, mais pour éviter les problèmes de calcul de la matrice de vue, on restreint les angles
|
// On modifie l'angle de la caméra grâce au déplacement relatif sur X de la souris
|
||||||
m_targetAngles.pitch = Nz::Clamp(m_targetAngles.pitch - event.deltaY * sensitivity, -89.f, 89.f);
|
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)
|
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();
|
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.SetPosition(currentPosition);
|
||||||
node.SetRotation(currentRotation);
|
node.SetRotation(currentRotation);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue