-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
197 lines (162 loc) · 5.05 KB
/
Camera.cpp
File metadata and controls
197 lines (162 loc) · 5.05 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "Camera.h"
#include "BoxCollider.h"
#include <iostream>
#include <cmath>
// chat gipity helped me with interpolation
Camera::Camera(int width, int height, glm::vec3 position)
:width(width),
height(height),
Position(position)
{}
float lerp(float a, float b, float t) {
return a + t * (b - a);
}
void Camera::updateMatrix(float FOVdeg, float nearPlane, float farPlane)
{
glm::mat4 view = glm::mat4(1.0f);
glm::mat4 projection = glm::mat4(1.0f);
view = glm::lookAt(Position, Position + Orientation, Up);
if (currentTilt != 0.0f)
{
glm::mat4 tilt = glm::rotate(glm::mat4(1.0f), currentTilt, glm::vec3(0.0f, 0.0f, 1.0f));
view = tilt * view;
}
// there was a problem where the aspect ratio was using int division so i updated this
projection = glm::perspective(glm::radians(FOVdeg), (float)width / (float)height, nearPlane, farPlane);
cameraMatrix = projection * view;
}
void Camera::RigidBody(float deltaTime)
{
Position.y -= Velocity * deltaTime;
//max velocity
if (Velocity < maxVelocity) {
Velocity += Acceleration * deltaTime;
}
// apply horizontal movement velocity
Position += moveVelocity * deltaTime;
// apply friction to slow down when not actively moving
float frictionFactor = 1.0f - std::min(moveFriction * deltaTime, 1.0f);
moveVelocity.x *= frictionFactor;
moveVelocity.z *= frictionFactor;
// clamp very small velocities to zero to prevent sliding
if (glm::length(glm::vec2(moveVelocity.x, moveVelocity.z)) < 0.01f) {
moveVelocity.x = 0.0f;
moveVelocity.z = 0.0f;
}
// for smoother interpolation between two values
if (fabs(currentTilt - targetTilt) > 0.0001f) {
float factor = std::min(1.0f, tiltSpeed * deltaTime);
currentTilt = lerp(currentTilt, targetTilt, factor);
}else if (currentTilt != targetTilt) {
currentTilt = targetTilt;
}
}
void Camera::Matrix(Shader& shader, const char* uniform)
{
glUniformMatrix4fv(glGetUniformLocation(shader.ID, uniform), 1, GL_FALSE, glm::value_ptr(cameraMatrix));
}
void Camera::Inputs(GLFWwindow* window, float deltaTime)
{
//beware shit code incoming
//but i'm too dumb to make it good
//Camera Movement
if (firstClick)
{
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double mouseX;
double mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
float rotX = sensitivity * (float)(mouseY - (height / 2)) / height;
float rotY = sensitivity * (float)(mouseX - (width / 2)) / width;
if (Orientation.y > 0.98f && rotX < 0.0f) rotX = 0.0f;
if (Orientation.y < -0.98f && rotX > 0.0f) rotX = 0.0f;
Orientation = glm::rotate(Orientation, glm::radians(-rotX), glm::normalize(glm::cross(Orientation, Up)));
Orientation = glm::rotate(Orientation, glm::radians(-rotY), Up);
glfwSetCursorPos(window, (width / 2), (height / 2));
}
glm::vec3 forward = glm::normalize(glm::vec3(Orientation.x, 0.0f, Orientation.z));
glm::vec3 side = glm::normalize(glm::cross(forward, Up));
glm::vec3 moveDir = glm::vec3(0.0f);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
moveDir += forward;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
moveDir -= forward;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
moveDir -= side;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
moveDir += side;
}
// normalize the movement direction if moving diagonally
if (glm::length(moveDir) > 0.0f)
{
moveDir = glm::normalize(moveDir);
moveVelocity += moveDir * moveAcceleration * deltaTime;
float horizontalSpeed = glm::length(glm::vec2(moveVelocity.x, moveVelocity.z));
if (horizontalSpeed > maxMoveSpeed)
{
moveVelocity.x = (moveVelocity.x / horizontalSpeed) * maxMoveSpeed;
moveVelocity.z = (moveVelocity.z / horizontalSpeed) * maxMoveSpeed;
}
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS && !glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
targetTilt = -tiltAmount;
}
else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS && !glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
targetTilt = tiltAmount;
}
else
{
targetTilt = 0.0f;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
{
if (alreadyJumped) return;
if (!firstSpace) firstSpace = true;
if (wasSpaceClicked) return;
Velocity = -JumpVelocity;
wasSpaceClicked = true;
alreadyJumped = true;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_RELEASE)
{
wasSpaceClicked = false;
}
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
{
if (!firstClick) firstClick = true;
if (wasMouseClicked) return;
wasMouseClicked = true;
}
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_RELEASE)
{
wasMouseClicked = false;
}
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
firstClick = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
void Camera::CollisionPush(BoxCollider* collider)
{
CollisionResult result = collider->CheckCollision(Position.x, Position.z, Position.y);
if (result.collided)
{
Position += glm::vec3(result.pushX, result.pushY, result.pushZ);
//if touched ground
if (result.pushZ >= 0)
{
Velocity = 1.0f;
alreadyJumped = false;
}
}
}