-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
78 lines (61 loc) · 1.98 KB
/
player.cpp
File metadata and controls
78 lines (61 loc) · 1.98 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "player.hpp"
#include "utils.hpp"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
#include <iostream>
Player::Player(glm::vec3 position, float pitch, float yaw) : position(position), pitch(pitch), yaw(yaw) {
velocity = glm::vec3(0.0f);
last_gravity = glm::vec3(0.0f);
}
void Player::process_input(float forward_offset, float pitch_offset, float yaw_offset) {
pitch += pitch_offset;
yaw += yaw_offset;
const float forward_sensitivity = 0.01f;
velocity += get_front() * forward_offset * forward_sensitivity;
}
void Player::add_gravity(glm::vec3 gravity) {
velocity += gravity;
last_gravity = gravity;
}
void Player::set_position(glm::vec3 position) {
this->position = position;
}
glm::vec3 Player::get_position() {
static float last_time = -1;
float time = get_time();
if (last_time < 0)
last_time = time;
float delta_time = time - last_time;
last_time = time;
const float multiplier = 512.0f;
position += velocity * multiplier * delta_time;
return position;
}
glm::vec3 Player::get_velocity() {
return velocity;
}
void Player::get_camera_vecs(glm::vec3 *front, glm::vec3 *right, glm::vec3 *up) {
*front = get_front();
const glm::vec3 WORLDUP(0.0f, 1.0f, 0.0f);
*right = glm::normalize(glm::cross(*front, WORLDUP));
if (cos(pitch) < 0)
*right *= (-1);
*up = glm::normalize(glm::cross(*right, *front));
}
glm::vec3 Player::get_front() {
glm::vec3 front;
front.x = cos(yaw) * cos(pitch);
front.y = sin(pitch);
front.z = sin(yaw) * cos(pitch);
front = glm::normalize(front);
return front;
}
void Player::draw_lines(glm::mat4 projection, glm::mat4 view, glm::vec3 planet_pos) {
const float multiplier = 5.0f;
glm::vec3 front_pos = position + get_front();
glm::vec3 velocity_pos = front_pos + velocity * multiplier;
draw_line(projection, view, front_pos, velocity_pos, glm::vec4(1, 1, 0, 1)); //yellow
draw_line(projection, view, velocity_pos, planet_pos, glm::vec4(1, 1, 1, 1)); //white
}