minecraftvulkan/src/keyboard_movement_controller.cpp

42 lines
1.7 KiB
C++
Raw Normal View History

2022-09-19 01:20:51 +00:00
#include "keyboard_movement_controller.hpp"
#include <glm/common.hpp>
#include <glm/fwd.hpp>
#include <glm/geometric.hpp>
#include <limits>
2022-09-20 01:28:41 +00:00
namespace app {
2022-09-19 01:20:51 +00:00
2022-09-25 01:16:13 +00:00
void KeyboardMovementController::moveInPlaneXZ(GLFWwindow* window, float dt, xe::GameObject& gameObject) {
2022-09-19 01:20:51 +00:00
glm::vec3 rotate{0};
if(glfwGetKey(window, keys.lookRight) == GLFW_PRESS) rotate.y += 1.f;
if(glfwGetKey(window, keys.lookLeft) == GLFW_PRESS) rotate.y -= 1.f;
if(glfwGetKey(window, keys.lookUp) == GLFW_PRESS) rotate.x -= 1.f;
if(glfwGetKey(window, keys.lookDown) == GLFW_PRESS) rotate.x += 1.f;
if (glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()) {
gameObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate);
}
gameObject.transform.rotation.x = glm::clamp(gameObject.transform.rotation.x, -1.5f, 1.5f);
gameObject.transform.rotation.y = glm::mod(gameObject.transform.rotation.y, glm::two_pi<float>());
float yaw = gameObject.transform.rotation.y;
const glm::vec3 forwardDir{sin(yaw), 0.f, cos(yaw)};
const glm::vec3 rightDir{forwardDir.z, 0.f, -forwardDir.x};
const glm::vec3 upDir{0.f, 01.f, 0.f};
glm::vec3 moveDir{0};
if(glfwGetKey(window, keys.moveForward) == GLFW_PRESS) moveDir += forwardDir;
if(glfwGetKey(window, keys.moveBackward) == GLFW_PRESS) moveDir -= forwardDir;
if(glfwGetKey(window, keys.moveRight) == GLFW_PRESS) moveDir += rightDir;
if(glfwGetKey(window, keys.moveLeft) == GLFW_PRESS) moveDir -= rightDir;
if(glfwGetKey(window, keys.moveUp) == GLFW_PRESS) moveDir += upDir;
if(glfwGetKey(window, keys.moveDown) == GLFW_PRESS) moveDir -= upDir;
if (glm::dot(moveDir, moveDir) > std::numeric_limits<float>::epsilon()) {
gameObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir);
}
}
}