-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyboard_movement_controller.cpp
More file actions
37 lines (29 loc) · 1.73 KB
/
keyboard_movement_controller.cpp
File metadata and controls
37 lines (29 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "keyboard_movement_controller.hpp"
namespace lve {
void KeyboardMovementController::moveInPlaneXZ(GLFWwindow* window, float deltaTime, LveGameObject& gameObject) {
glm::vec3 rotate{ 0.0f };
if (glfwGetKey(window, keys.lookRight) == GLFW_PRESS) rotate.y += 1.0f;
if (glfwGetKey(window, keys.lookLeft) == GLFW_PRESS) rotate.y -= 1.0f;
if (glfwGetKey(window, keys.lookUp) == GLFW_PRESS) rotate.x += 1.0f;
if (glfwGetKey(window, keys.lookDown) == GLFW_PRESS) rotate.x -= 1.0f;
if (glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()) {
gameObject.transform.rotation += lookSpeed * deltaTime * 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.0f, cos(yaw) };
const glm::vec3 rightDir{ forwardDir.z, 0.0f, -forwardDir.x };
const glm::vec3 upDir{ 0.0f, -1.0f, 0.0f };
glm::vec3 moveDir{ 0.0f };
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 * deltaTime * glm::normalize(moveDir);
}
}
}