-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraController.cpp
109 lines (88 loc) · 2.52 KB
/
CameraController.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "CameraController.h"
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/System/Vector3.hpp>
#include <iostream>
#include <cmath>
using namespace std;
#include "Vector3f.h"
#include "Constants.h"
CameraController::CameraController(float viewportWidth, float viewportHeight)
: camera(new Camera(viewportWidth, viewportHeight))
, speed(CAM_UNIT_SPEED)
{}
float CameraController::getSpeed() const
{
return speed;
}
void CameraController::update(const float dt)
{}
void CameraController::keyHeld(const sf::Keyboard::Key &key, const float dt)
{
float velocity = speed * max(0.00001f, camera->scale());
float offset = velocity * dt;
switch (key)
{
// Move forwards
case sf::Keyboard::W:
{
Vector3f tmp(camera->direction);
tmp.normalize();
tmp *= abs(offset);
camera->position += tmp;
}
break;
// Move backwards
case sf::Keyboard::S:
{
Vector3f tmp(camera->direction);
tmp.normalize();
tmp *= abs(offset);
camera->position -= tmp;
}
break;
// Move left
case sf::Keyboard::A:
{
Vector3f tmp(camera->direction);
tmp.cross(camera->up).normalize();
tmp *= abs(offset);
camera->position += tmp;
}
break;
// Move right
case sf::Keyboard::D:
{
Vector3f tmp(camera->up);
tmp.cross(camera->direction).normalize();
tmp *= abs(offset);
camera->position += tmp;
}
break;
default:
break;
}
}
const float clamp(const float &n, const float &lower, const float &upper) {
return std::max(lower, std::min(n, upper));
}
void CameraController::mouseMoved(const int mouseX, const int mouseY, const int dx, const int dy, const float dt)
{
float deltaX = (float)dx * CAM_DEGREES_PER_PIXEL;
float deltaY = (float)dy * CAM_DEGREES_PER_PIXEL;
camera->direction.rotate(camera->up, deltaX);
Vector3f right(camera->direction);
right.cross(camera->up).normalize();
camera->direction.rotate(right, deltaY);
camera->up.rotate(right, deltaY);
}
void CameraController::mouseScrolled(const float scrollDelta, const int mouseX, const int mouseY, const float timeDelta)
{
speed += scrollDelta*0.2f;
speed = max(0.1f, speed);
}
// Unused functions
void CameraController::keyPressed(const sf::Keyboard::Key &key, const float dt) {}
void CameraController::keyReleased(const sf::Keyboard::Key &key, const float dt) {}
void CameraController::mouseReleased(const sf::Mouse::Button &btn, const int mouseX, const int mouseY, const float dt) {}
void CameraController::mousePressed(const sf::Mouse::Button &btn, const int mouseX, const int mouseY, const float dt) {}