1 #include "controller.hpp"
3 #include <glm/gtx/euler_angles.hpp>
4 #include <glm/gtx/rotate_vector.hpp>
9 FPSController::FPSController(Entity &entity)
13 , move_velocity(0.005f)
14 , pitch_sensitivity(-0.0025f)
15 , yaw_sensitivity(-0.001f)
26 void FPSController::Pitch(float p) {
30 } else if (pitch < -PI / 2) {
35 void FPSController::RotatePitch(float delta) {
39 void FPSController::Yaw(float y) {
43 } else if (yaw < -PI) {
48 void FPSController::RotateYaw(float delta) {
53 void FPSController::HandleKeyboard(const SDL_KeyboardEvent &event) {
54 switch (event.keysym.sym) {
56 front = event.state == SDL_PRESSED;
59 back = event.state == SDL_PRESSED;
62 left = event.state == SDL_PRESSED;
65 right = event.state == SDL_PRESSED;
69 up = event.state == SDL_PRESSED;
73 down = event.state == SDL_PRESSED;
78 void FPSController::HandleMouse(const SDL_MouseMotionEvent &event) {
79 RotateYaw(event.xrel * yaw_sensitivity);
80 RotatePitch(event.yrel * pitch_sensitivity);
84 void FPSController::Update(int dt) {
87 vel.x = move_velocity;
88 } else if (left && !right) {
89 vel.x = -move_velocity;
92 vel.y = move_velocity;
93 } else if (down && !up) {
94 vel.y = -move_velocity;
97 vel.z = move_velocity;
98 } else if (front && !back) {
99 vel.z = -move_velocity;
101 entity.Rotation(glm::eulerAngleYX(yaw, pitch));
102 entity.Velocity(glm::rotateY(vel, yaw));