Moving camera along a custom plane

This commit is contained in:
Vladyslav Baranovskyi
2024-07-01 11:52:57 +03:00
parent 55eb910ff5
commit 4035d10c12
2 changed files with 29 additions and 20 deletions

View File

@@ -17,9 +17,10 @@ namespace OpenVulkano::Scene
{ {
auto input = OpenVulkano::Input::InputManager::GetInstance(); auto input = OpenVulkano::Input::InputManager::GetInstance();
m_actionForward = input->GetAction("forward"); m_actionForward = input->GetAction("forward");
m_actionSide = input->GetAction("side"); m_actionBackward = input->GetAction("backward");
m_actionLeft = input->GetAction("left");
m_actionRight = input->GetAction("right");
m_actionUp = input->GetAction("up"); m_actionUp = input->GetAction("up");
m_actionBoost = input->GetAction("boost");
} }
void PlaneCameraController::Init(Camera* camera, const Math::Vector3f& planeNormal) void PlaneCameraController::Init(Camera* camera, const Math::Vector3f& planeNormal)
@@ -45,18 +46,20 @@ namespace OpenVulkano::Scene
{ {
auto input = OpenVulkano::Input::InputManager::GetInstance(); auto input = OpenVulkano::Input::InputManager::GetInstance();
Math::Vector3f direction(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), Math::Vector3f direction(0.0f, 0.0f, 0.0f);
-input->GetAxis(m_actionForward));
if (input->GetButton(m_actionForward)) { direction += m_planeForward; }
if (input->GetButton(m_actionBackward)) { direction -= m_planeForward; }
if (input->GetButton(m_actionLeft)) { direction -= m_planeRight; }
if (input->GetButton(m_actionRight)) { direction += m_planeRight; }
if (Math::Utils::length2(direction) > 0.0f) { direction = Math::Utils::normalize(direction); } if (Math::Utils::length2(direction) > 0.0f) { direction = Math::Utils::normalize(direction); }
const float SPEED = 3.0f; float timeScale = CURRENT_FRAME.frameTime;
const float BOOST_FACTOR = 3.0; float speed = 3.0f;
direction *= timeScale * speed;
if (input->GetButton(m_actionBoost)) { direction *= BOOST_FACTOR; } direction = direction - Math::Utils::dot(direction, m_planeNormal) * m_planeNormal;
m_position += direction;
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); Math::Matrix4f transformation = Math::Utils::translate(m_position);
GetCamera()->SetMatrix(transformation); GetCamera()->SetMatrix(transformation);
@@ -64,17 +67,20 @@ namespace OpenVulkano::Scene
void PlaneCameraController::SetDefaultKeybindings() void PlaneCameraController::SetDefaultKeybindings()
{ {
m_actionForward->BindKey(Input::InputKey::Controller::AXIS_LEFT_Y); m_actionForward->BindKey(Input::InputKey::Keyboard::KEY_W);
m_actionForward->BindAxisButtons(Input::InputKey::Keyboard::KEY_W, Input::InputKey::Keyboard::KEY_S); m_actionBackward->BindKey(Input::InputKey::Keyboard::KEY_S);
m_actionForward->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, -0.0025f); m_actionLeft->BindKey(Input::InputKey::Keyboard::KEY_A);
m_actionSide->BindKey(Input::InputKey::Controller::AXIS_LEFT_X); m_actionRight->BindKey(Input::InputKey::Keyboard::KEY_D);
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); m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL);
} }
void PlaneCameraController::SetPlaneNormal(const Math::Vector3f& planeNormal) void PlaneCameraController::SetPlaneNormal(const Math::Vector3f& planeNormal)
{ {
m_planeNormal = Math::Utils::normalize(planeNormal); m_planeNormal = Math::Utils::normalize(planeNormal);
Math::Vector3f arbitraryVector = (fabs(m_planeNormal.x) > 0.9f) ? Math::Vector3f(0.0f, 1.0f, 0.0f) :
Math::Vector3f(1.0f, 0.0f, 0.0f);
m_planeRight = Math::Utils::normalize(Math::Utils::cross(m_planeNormal, arbitraryVector));
m_planeForward = Math::Utils::normalize(Math::Utils::cross(m_planeNormal, m_planeRight));
} }
} }

View File

@@ -16,10 +16,13 @@ namespace OpenVulkano::Scene
{ {
Math::Vector3f m_planeNormal; Math::Vector3f m_planeNormal;
Math::Vector3f m_position; Math::Vector3f m_position;
Math::Vector3f m_planeRight;
Math::Vector3f m_planeForward;
Input::InputAction* m_actionForward; Input::InputAction* m_actionForward;
Input::InputAction* m_actionSide; Input::InputAction* m_actionBackward;
Input::InputAction* m_actionLeft;
Input::InputAction* m_actionRight;
Input::InputAction* m_actionUp; Input::InputAction* m_actionUp;
Input::InputAction* m_actionBoost;
public: public:
enum class DefaultAxis enum class DefaultAxis