Update ArcballCameraController

This commit is contained in:
Georg Hagen
2024-06-24 16:49:56 +02:00
parent f011a0a4f5
commit 61929e61d3
3 changed files with 25 additions and 17 deletions

View File

@@ -25,21 +25,18 @@ namespace OpenVulkano
void ArcballCameraController::Tick()
{
if (!GetCamera()) return;
auto input = Input::InputManager::GetInstance();
Input::InputManager* input = Input::InputManager::GetInstance();
Math::Vector4f_SIMD position = GetCamera()->GetPosition();
float yaw = input->GetAxis(m_actionLookSide);
float pitch = input->GetAxis(m_actionLookUp);
yaw += input->GetAxis(m_actionLookSide);
pitch += input->GetAxis(m_actionLookUp);
// Handle issue with camera direction being the same as the up vector
float cosAngle = dot(Math::Vector3f_SIMD(GetCamera()->GetViewDirection()), m_upVector);
if (cosAngle * Math::Utils::sign(pitch) > 0.99f) pitch = 0;
pitch = std::min(1.5f, std::max(-1.5f, pitch));
// Rotate the camera around the pivot point
Math::Matrix4f rotationMatrixX = Math::Utils::rotate(yaw, m_upVector);
position = (rotationMatrixX * (position - m_pivotPoint)) + m_pivotPoint;
m_rightVector = Math::Vector3f_SIMD(rotationMatrixX * Math::Vector4f(m_rightVector, 0));
Math::Matrix4f rotationMatrixY = Math::Utils::rotate(pitch, m_rightVector);
position = (rotationMatrixY * (position - m_pivotPoint)) + m_pivotPoint;
Math::Matrix4f rotateY = Math::Utils::rotate(yaw, Math::Vector3f_SIMD(0, 1, 0));
Math::Vector4f x = rotateY * Math::Vector4f(1, 0, 0, 0);
Math::Matrix4f rotateX = Math::Utils::rotate(pitch, Math::Vector3f_SIMD(x));
position = rotateX * rotateY * Math::Vector4f_SIMD(0, 0, 3, 0);
position += m_pivotPoint;
// Move the camera and the pivot point
Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), -input->GetAxis(m_actionForward));
@@ -60,12 +57,16 @@ namespace OpenVulkano
// Update the camera view
GetCamera()->SetViewMatrix(Math::Utils::lookAt(reinterpret_cast<Math::Vector3f_SIMD&>(position),
reinterpret_cast<Math::Vector3f_SIMD&>(m_pivotPoint),
m_upVector));
Math::Vector3f_SIMD(0,1,0)));
}
void ArcballCameraController::SetActive()
{
m_pivotPoint = GetCamera()->GetPosition() + Math::Vector4f(GetCamera()->GetViewDirection() * 3.0f, 0);
Math::Vector3f viewDir = Math::Utils::normalize(GetCamera()->GetViewDirection());
m_pivotPoint = GetCamera()->GetPosition() + Math::Vector4f(viewDir * 3.0f, 0);
pitch = Math::Utils::asin(viewDir.y);
yaw = Math::Utils::atan(-viewDir.x, -viewDir.z);
}
void ArcballCameraController::SetDefaultKeybindings()
@@ -80,7 +81,7 @@ namespace OpenVulkano
m_actionUp->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, 0.03f);
m_actionLookUp->BindKey(Input::InputKey::Controller::AXIS_RIGHT_Y);
m_actionLookUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_DOWN, Input::InputKey::Keyboard::KEY_UP);
m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, 0.001f);
m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, -0.001f);
m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X);
m_actionLookSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_RIGHT, Input::InputKey::Keyboard::KEY_LEFT);
m_actionLookSide->BindKey(Input::InputKey::Touch::AXIS_PAN_X, -0.001f);