]> git.localhorst.tv Git - blank.git/blob - src/controller.cpp
split entity from controller
[blank.git] / src / controller.cpp
1 #include "controller.hpp"
2
3 #include <glm/gtx/euler_angles.hpp>
4 #include <glm/gtx/rotate_vector.hpp>
5
6
7 namespace blank {
8
9 FPSController::FPSController(Entity &entity)
10 : entity(entity)
11 , pitch(0)
12 , yaw(0)
13 , move_velocity(0.003f)
14 , pitch_sensitivity(-0.0025f)
15 , yaw_sensitivity(-0.001f)
16 , front(false)
17 , back(false)
18 , left(false)
19 , right(false)
20 , up(false)
21 , down(false) {
22
23 }
24
25
26 void FPSController::Pitch(float p) {
27         pitch = p;
28         if (pitch > PI / 2) {
29                 pitch = PI / 2;
30         } else if (pitch < -PI / 2) {
31                 pitch = -PI / 2;
32         }
33 }
34
35 void FPSController::RotatePitch(float delta) {
36         Pitch(pitch + delta);
37 }
38
39 void FPSController::Yaw(float y) {
40         yaw = y;
41         if (yaw > PI) {
42                 yaw -= PI * 2;
43         } else if (yaw < -PI) {
44                 yaw += PI * 2;
45         }
46 }
47
48 void FPSController::RotateYaw(float delta) {
49         Yaw(yaw + delta);
50 }
51
52
53 void FPSController::HandleKeyboard(const SDL_KeyboardEvent &event) {
54         switch (event.keysym.sym) {
55                 case SDLK_w:
56                         front = event.state == SDL_PRESSED;
57                         break;
58                 case SDLK_s:
59                         back = event.state == SDL_PRESSED;
60                         break;
61                 case SDLK_a:
62                         left = event.state == SDL_PRESSED;
63                         break;
64                 case SDLK_d:
65                         right = event.state == SDL_PRESSED;
66                         break;
67                 case SDLK_q:
68                 case SDLK_SPACE:
69                         up = event.state == SDL_PRESSED;
70                         break;
71                 case SDLK_e:
72                 case SDLK_LSHIFT:
73                         down = event.state == SDL_PRESSED;
74                         break;
75         }
76 }
77
78 void FPSController::HandleMouse(const SDL_MouseMotionEvent &event) {
79         RotateYaw(event.xrel * yaw_sensitivity);
80         RotatePitch(event.yrel * pitch_sensitivity);
81 }
82
83
84 void FPSController::Update(int dt) {
85         glm::vec3 vel;
86         if (right && !left) {
87                 vel.x = move_velocity;
88         } else if (left && !right) {
89                 vel.x = -move_velocity;
90         }
91         if (up && !down) {
92                 vel.y = move_velocity;
93         } else if (down && !up) {
94                 vel.y = -move_velocity;
95         }
96         if (back && !front) {
97                 vel.z = move_velocity;
98         } else if (front && !back) {
99                 vel.z = -move_velocity;
100         }
101         entity.Rotation(glm::eulerAngleYX(yaw, pitch));
102         entity.Velocity(glm::rotateY(vel, yaw));
103 }
104
105 }