-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter.cpp
More file actions
116 lines (97 loc) · 3.32 KB
/
Copy pathcharacter.cpp
File metadata and controls
116 lines (97 loc) · 3.32 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "character.h"
#include "gameobject.h"
#include "glm/gtc/matrix_transform.hpp"
Character::Character(const glm::vec3& pos, const glm::vec3& siz, float radius) : GameObject{pos, siz}, Front{glm::vec3{0.0f, 0.0f, -1.0f}}, Forward{glm::vec3{0.0f, 0.0f, -1.0f}}, Up{glm::vec3{0.0f, 1.0f, 0.0f}}, Right{}, WorldUp{Up}, Yaw{-90.0f}, Pitch{0.0f}, MovementSpeed{0.035f}, MouseSensitivity{0.1f}, Fov{85.0f}, Radius{radius}, Grounded{false}
{
updateDirectionVectors();
}
// Respond tp keyboard input received from game instance
void Character::processKeyboard(PlayerMovement direction)
{
// Move the character on the 2D plane
if (direction == PlayerMovement::FORWARD)
addVelocity(Forward * MovementSpeed);
if (direction == PlayerMovement::BACKWARD)
addVelocity(-Forward * MovementSpeed);
if (direction == PlayerMovement::RIGHT)
addVelocity(Right * MovementSpeed);
if (direction == PlayerMovement::LEFT)
addVelocity(-Right * MovementSpeed);
// Values for jumping logic
constexpr auto startingJumpVel{0.25f};
static auto jumping{false};
static auto jumpPressed{false};
static auto jumpCount{0};
static auto jumpVel{startingJumpVel};
if (direction == PlayerMovement::JUMP_PRESSED && Grounded && !jumpPressed)
{
jumping = true;
Grounded = false;
jumpPressed = true;
}
// If jump button is released at this point, it won't take affect until the next iteration
if (direction == PlayerMovement::JUMP_RELEASED)
{
jumpPressed = false;
}
// Climb until velocity is lost
if (jumping)
{
addVelocity(WorldUp * jumpVel);
++jumpCount;
jumpVel *= 0.95f;
if (jumpCount >= 180)
{
jumping = false;
jumpCount = 0;
jumpVel = startingJumpVel;
}
}
// If land early, reset jumping state
if (Grounded)
{
jumping = false;
jumpCount = 0;
jumpVel = startingJumpVel;
}
}
// Handle mouse input received from game instance
void Character::processMouseMovement(float xOffset, float yOffset)
{
Yaw += xOffset * MouseSensitivity;
Pitch += yOffset * MouseSensitivity;
// Constrain pitch to prevent screen flipping
constexpr auto pitchMax{89.0f};
constexpr auto pitchMin{-89.0f};
if (Pitch > pitchMax)
Pitch = pitchMax;
if (Pitch < pitchMin)
Pitch = pitchMin;
// Update Front, Right, and Up Vectors using the updated Euler angles
updateDirectionVectors();
}
// Calculate and return the view matrix based on the player character's current state
glm::mat4 Character::getViewMatrix() const
{
return lookAt(position_, position_ + Front, Up);
}
void Character::tick(float deltaTime)
{
move();
}
// Update direction vectors for the camera view and player character movement
void Character::updateDirectionVectors()
{
// Calculate new front vector for camera
const auto dirX{cos(glm::radians(Yaw)) * cos(glm::radians(Pitch))};
const auto dirY{sin(glm::radians(Pitch))};
const auto dirZ{sin(glm::radians(Yaw)) * cos(glm::radians(Pitch))};
const auto front{glm::vec3{dirX, dirY, dirZ}};
Front = normalize(front);
// Calculate new forward vector for character movement -- Y is zero to prevent flying
const auto forward{glm::vec3{dirX, 0.0f, dirZ}};
Forward = normalize(forward);
// Calculate new the right and up vectors
Right = normalize(cross(Front, WorldUp)); // Normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement
Up = normalize(cross(Right, Front));
}