1 #include "controller.hpp"
3 #include <glm/gtc/matrix_transform.hpp>
4 #include <glm/gtx/euler_angles.hpp>
5 #include <glm/gtx/rotate_vector.hpp>
6 #include <glm/gtx/transform.hpp>
11 FPSController::FPSController()
18 , move_velocity(0.003f)
19 , pitch_sensitivity(-0.0025f)
20 , yaw_sensitivity(-0.001f)
31 const glm::mat4 &FPSController::Transform() const {
33 transform = glm::translate(position) * glm::eulerAngleYX(yaw, pitch);
39 Ray FPSController::Aim() const {
40 glm::vec4 from = Transform() * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
42 glm::vec4 to = Transform() * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
44 return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
48 void FPSController::OrientationVelocity(const glm::vec3 &vel) {
49 Velocity(glm::rotateY(vel, yaw));
53 void FPSController::Pitch(float p) {
57 } else if (pitch < -PI / 2) {
63 void FPSController::RotatePitch(float delta) {
67 void FPSController::Yaw(float y) {
71 } else if (yaw < -PI) {
77 void FPSController::RotateYaw(float delta) {
82 void FPSController::HandleKeyboard(const SDL_KeyboardEvent &event) {
83 switch (event.keysym.sym) {
85 front = event.state == SDL_PRESSED;
88 back = event.state == SDL_PRESSED;
91 left = event.state == SDL_PRESSED;
94 right = event.state == SDL_PRESSED;
98 up = event.state == SDL_PRESSED;
102 down = event.state == SDL_PRESSED;
107 void FPSController::HandleMouse(const SDL_MouseMotionEvent &event) {
108 RotateYaw(event.xrel * yaw_sensitivity);
109 RotatePitch(event.yrel * pitch_sensitivity);
113 void FPSController::Update(int dt) {
115 if (right && !left) {
116 vel.x = move_velocity;
117 } else if (left && !right) {
118 vel.x = -move_velocity;
121 vel.y = move_velocity;
122 } else if (down && !up) {
123 vel.y = -move_velocity;
125 if (back && !front) {
126 vel.z = move_velocity;
127 } else if (front && !back) {
128 vel.z = -move_velocity;
130 OrientationVelocity(vel);
132 Move(velocity * float(dt));