]> git.localhorst.tv Git - blank.git/blob - src/controller.cpp
3911e0f26c5c0716226a9303ae3c6411dc56bb40
[blank.git] / src / controller.cpp
1 #include "controller.hpp"
2
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>
7
8
9 namespace blank {
10
11 FPSController::FPSController()
12 : velocity(0, 0, 0)
13 , position(0, 0, 0)
14 , pitch(0)
15 , yaw(0)
16 , transform(1.0f)
17 , dirty(true)
18 , move_velocity(0.003f)
19 , pitch_sensitivity(-0.0025f)
20 , yaw_sensitivity(-0.001f)
21 , front(false)
22 , back(false)
23 , left(false)
24 , right(false)
25 , up(false)
26 , down(false) {
27
28 }
29
30
31 const glm::mat4 &FPSController::Transform() const {
32         if (dirty) {
33                 transform = glm::translate(position) * glm::eulerAngleYX(yaw, pitch);
34                 dirty = false;
35         }
36         return transform;
37 }
38
39 Ray FPSController::Aim() const {
40         glm::vec4 from = Transform() * glm::vec4(0.0f, 0.0f, 1.0f, 1.0f);
41         from /= from.w;
42         glm::vec4 to = Transform() * glm::vec4(0.0f, 0.0f, -1.0f, 1.0f);
43         to /= to.w;
44         return Ray{ glm::vec3(from), glm::normalize(glm::vec3(to - from)) };
45 }
46
47
48 void FPSController::OrientationVelocity(const glm::vec3 &vel) {
49         Velocity(glm::rotateY(vel, yaw));
50 }
51
52
53 void FPSController::Pitch(float p) {
54         pitch = p;
55         if (pitch > PI / 2) {
56                 pitch = PI / 2;
57         } else if (pitch < -PI / 2) {
58                 pitch = -PI / 2;
59         }
60         dirty = true;
61 }
62
63 void FPSController::RotatePitch(float delta) {
64         Pitch(pitch + delta);
65 }
66
67 void FPSController::Yaw(float y) {
68         yaw = y;
69         if (yaw > PI) {
70                 yaw -= PI * 2;
71         } else if (yaw < -PI) {
72                 yaw += PI * 2;
73         }
74         dirty = true;
75 }
76
77 void FPSController::RotateYaw(float delta) {
78         Yaw(yaw + delta);
79 }
80
81
82 void FPSController::HandleKeyboard(const SDL_KeyboardEvent &event) {
83         switch (event.keysym.sym) {
84                 case SDLK_w:
85                         front = event.state == SDL_PRESSED;
86                         break;
87                 case SDLK_s:
88                         back = event.state == SDL_PRESSED;
89                         break;
90                 case SDLK_a:
91                         left = event.state == SDL_PRESSED;
92                         break;
93                 case SDLK_d:
94                         right = event.state == SDL_PRESSED;
95                         break;
96                 case SDLK_q:
97                 case SDLK_SPACE:
98                         up = event.state == SDL_PRESSED;
99                         break;
100                 case SDLK_e:
101                 case SDLK_LSHIFT:
102                         down = event.state == SDL_PRESSED;
103                         break;
104         }
105 }
106
107 void FPSController::HandleMouse(const SDL_MouseMotionEvent &event) {
108         RotateYaw(event.xrel * yaw_sensitivity);
109         RotatePitch(event.yrel * pitch_sensitivity);
110 }
111
112
113 void FPSController::Update(int dt) {
114         glm::vec3 vel;
115         if (right && !left) {
116                 vel.x = move_velocity;
117         } else if (left && !right) {
118                 vel.x = -move_velocity;
119         }
120         if (up && !down) {
121                 vel.y = move_velocity;
122         } else if (down && !up) {
123                 vel.y = -move_velocity;
124         }
125         if (back && !front) {
126                 vel.z = move_velocity;
127         } else if (front && !back) {
128                 vel.z = -move_velocity;
129         }
130         OrientationVelocity(vel);
131
132         Move(velocity * float(dt));
133 }
134
135 }