1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#include <GLFW/glfw3.h>
#include <math.h>
#include "player.h"
void move_player(Player *player, GLFWwindow* window, double* mouse_x, double* mouse_y, double width, double height) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
player->pos_x += player->dir_x * player->move_speed;
player->pos_y += player->dir_y * player->move_speed;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
player->pos_x -= player->dir_x * player->move_speed;
player->pos_y -= player->dir_y * player->move_speed;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
player->pos_x += player->dir_y * player->move_speed;
player->pos_y -= player->dir_x * player->move_speed;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
player->pos_x -= player->dir_y * player->move_speed;
player->pos_y += player->dir_x * player->move_speed;
}
// mouse controls (including y-shearing angle calculations
// DO NOT TOUCH, THIS TOOK A WHOLE NIGHT TO FIGURE OUT
double mouse_dx, mouse_dy;
double old_mouse_x, old_mouse_y;
old_mouse_x = *mouse_x;
old_mouse_y = *mouse_y;
glfwGetCursorPos(window, mouse_x, mouse_y);
mouse_dx = *mouse_x - old_mouse_x;
mouse_dy = *mouse_y - old_mouse_y;
double rot_speed = mouse_dx * player->sensitivity;
double old_dir_x = player->dir_x;
player->dir_x = player->dir_x * cos(rot_speed) - player->dir_y * sin(rot_speed);
player->dir_y = old_dir_x * sin(rot_speed) + player->dir_y * cos(rot_speed);
double old_plane_x = player->plane_x;
player->plane_x = player->plane_x * cos(rot_speed) - player->plane_y * sin(rot_speed);
player->plane_y = old_plane_x * sin(rot_speed) + player->plane_y * cos(rot_speed);
player->look_angle += mouse_dy * player->sensitivity;
if (player->look_angle > M_PI/4) { player->look_angle = M_PI/4; }
if (player->look_angle < -M_PI/4) { player->look_angle = -M_PI/4; }
}
|