minecraftvulkan/src/player_controller.cpp

43 lines
1.6 KiB
C++
Raw Normal View History

2022-10-01 18:35:16 +00:00
#include "player_controller.hpp"
2022-09-19 01:20:51 +00:00
2022-09-20 01:28:41 +00:00
namespace app {
2022-09-19 01:20:51 +00:00
2022-10-01 18:35:16 +00:00
PlayerController::PlayerController(xe::Input &input, xe::GameObject &viewerObject)
2022-09-25 23:05:56 +00:00
: input{input}, viewerObject{viewerObject} {};
2022-10-01 18:35:16 +00:00
PlayerController::~PlayerController() {};
2022-09-25 23:05:56 +00:00
2022-10-01 18:35:16 +00:00
void PlayerController::update(float dt) {
2022-09-19 01:20:51 +00:00
glm::vec3 rotate{0};
if(input.isKeyPressed(keys.lookRight)) rotate.y += 1.f;
if(input.isKeyPressed(keys.lookLeft)) rotate.y -= 1.f;
if(input.isKeyPressed(keys.lookUp)) rotate.x -= 1.f;
if(input.isKeyPressed(keys.lookDown)) rotate.x += 1.f;
2022-09-19 01:20:51 +00:00
if (glm::dot(rotate, rotate) > std::numeric_limits<float>::epsilon()) {
2022-09-25 23:05:56 +00:00
viewerObject.transform.rotation += lookSpeed * dt * glm::normalize(rotate);
2022-09-19 01:20:51 +00:00
}
2022-09-25 23:05:56 +00:00
viewerObject.transform.rotation.x = glm::clamp(viewerObject.transform.rotation.x, -1.5f, 1.5f);
viewerObject.transform.rotation.y = glm::mod(viewerObject.transform.rotation.y, glm::two_pi<float>());
2022-09-19 01:20:51 +00:00
2022-09-25 23:05:56 +00:00
float yaw = viewerObject.transform.rotation.y;
2022-09-19 01:20:51 +00:00
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(input.isKeyPressed(keys.moveForward)) moveDir += forwardDir;
if(input.isKeyPressed(keys.moveBackward)) moveDir -= forwardDir;
if(input.isKeyPressed(keys.moveRight)) moveDir += rightDir;
if(input.isKeyPressed(keys.moveLeft)) moveDir -= rightDir;
if(input.isKeyPressed(keys.moveUp)) moveDir += upDir;
if(input.isKeyPressed(keys.moveDown)) moveDir -= upDir;
2022-09-19 01:20:51 +00:00
if (glm::dot(moveDir, moveDir) > std::numeric_limits<float>::epsilon()) {
2022-09-25 23:05:56 +00:00
viewerObject.transform.translation += moveSpeed * dt * glm::normalize(moveDir);
2022-09-19 01:20:51 +00:00
}
}
}