-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraBehaviors.cpp
85 lines (63 loc) · 2.26 KB
/
CameraBehaviors.cpp
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
#include "CameraBehaviors.hpp"
#include <glm/common.hpp>
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
StationaryCamera::StationaryCamera(glm::vec3 position) {
pos = position;
}
void StationaryCamera::Update(float deltaTime) { }
void StationaryCamera::LookAt(glm::vec3 lookAt, glm::vec3 up) {
Camera::LookAt(lookAt,up);
}
HelicopterCamera::HelicopterCamera(float height, float radius, float speed, float focusHeight) {
currentAngle = 0;
this->radius = radius;
this->speed = speed;
pos.y = height;
Update(0);
LookAt(glm::vec3{0,focusHeight,0});
}
void HelicopterCamera::Update(float deltaTime) {
currentAngle += deltaTime * speed;
if (currentAngle > glm::radians(360.f)) {
currentAngle -= glm::radians(360.f);
}
pos.x = sinf(currentAngle) * radius;
pos.z = cosf(currentAngle) * radius;
}
FirstPersonCamera::FirstPersonCamera(glm::vec3 startPosition, float movementSpeed, float rotationSpeed):
movementSpeed(movementSpeed), rotationSpeed(rotationSpeed) {
pos = startPosition;
eulerAngles = glm::eulerAngles(rot);
startPos = pos;
startEulers = eulerAngles;
}
const glm::vec3 RIGHT{1.0,0,0};
const glm::vec3 UP{0,1.0,0};
const glm::vec3 FORWARD{0,0,1.0};
void FirstPersonCamera::Update(float deltaTime) {
if ((input & (int)Input::LookUp) != (input & (int)Input::LookDown)) {
glm::vec3 axisAndDirection = (input & (int)Input::LookUp) ? -RIGHT : RIGHT;
eulerAngles += rotationSpeed * deltaTime * axisAndDirection;
eulerAngles.x = glm::clamp(eulerAngles.x, glm::radians(90.f), glm::radians(270.f));
}
if ((input & (int)Input::LookLeft) != (input & (int)Input::LookRight)) {
glm::vec3 axisAndDirection = (input & (int)Input::LookLeft) ? -UP : UP;
eulerAngles += rotationSpeed * deltaTime * axisAndDirection;
}
rot = glm::quat{eulerAngles};
if ((input & (int)Input::MoveForward) != (input & (int)Input::MoveBackward)) {
glm::vec3 direction = (input & (int)Input::MoveForward) ? FORWARD : -FORWARD;
pos += movementSpeed * deltaTime * glm::rotate(rot, direction);
}
}
void FirstPersonCamera::PressedButton(Input button) {
input |= (int)button;
}
void FirstPersonCamera::ReleasedButton(Input button) {
input &= ~(int)button;
}
void FirstPersonCamera::Reset() {
pos = startPos;
eulerAngles = startEulers;
}