/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "PlaneCameraController.hpp" #include "Base/FrameMetadata.hpp" #include "Input/InputManager.hpp" #include "Input/InputKey.hpp" #include "Scene/Camera.hpp" namespace OpenVulkano::Scene { PlaneCameraController::PlaneCameraController(Camera* camera) : CameraController(camera), m_planeNormal(0.0f, 1.0f, 0.0f), m_position(0.0f, 0.0f, 0.0f) { auto input = OpenVulkano::Input::InputManager::GetInstance(); m_actionForward = input->GetAction("forward"); m_actionSide = input->GetAction("side"); m_actionUp = input->GetAction("up"); m_actionBoost = input->GetAction("boost"); } void PlaneCameraController::Init(Camera* camera, const Math::Vector3f& planeNormal) { CameraController::Init(camera); SetPlaneNormal(planeNormal); SetDefaultKeybindings(); } void PlaneCameraController::Init(Camera* camera, DefaultAxis axis) { Math::Vector3f vector; switch (axis) { case DefaultAxis::OXY: vector = Math::Vector3f(0, 0, 1); break; case DefaultAxis::OXZ: vector = Math::Vector3f(0, 1, 0); break; case DefaultAxis::OYZ: vector = Math::Vector3f(1, 0, 0); break; } Init(camera, vector); } void PlaneCameraController::Tick() { auto input = OpenVulkano::Input::InputManager::GetInstance(); Math::Vector3f direction(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), -input->GetAxis(m_actionForward)); if (Math::Utils::length2(direction) > 0.0f) { direction = Math::Utils::normalize(direction); } const float SPEED = 3.0f; const float BOOST_FACTOR = 3.0; if (input->GetButton(m_actionBoost)) { direction *= BOOST_FACTOR; } float dt = CURRENT_FRAME.frameTime; Math::Vector3f projDirection = direction - (Math::Utils::dot(direction, m_planeNormal) * m_planeNormal); m_position += projDirection * dt * SPEED; Math::Matrix4f transformation = Math::Utils::translate(m_position); GetCamera()->SetMatrix(transformation); } void PlaneCameraController::SetDefaultKeybindings() { m_actionForward->BindKey(Input::InputKey::Controller::AXIS_LEFT_Y); m_actionForward->BindAxisButtons(Input::InputKey::Keyboard::KEY_W, Input::InputKey::Keyboard::KEY_S); m_actionForward->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, -0.0025f); m_actionSide->BindKey(Input::InputKey::Controller::AXIS_LEFT_X); m_actionSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_D, Input::InputKey::Keyboard::KEY_A); m_actionSide->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_X, 0.0025f); m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL); } void PlaneCameraController::SetPlaneNormal(const Math::Vector3f& planeNormal) { m_planeNormal = Math::Utils::normalize(planeNormal); } }