-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththirdpersonfollower.cpp
102 lines (76 loc) · 2.57 KB
/
thirdpersonfollower.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
/*
* File: thirdpersonfollower.cpp
* Author: gg
*
* Created on 4 de dezembro de 2019, 17:23
*/
#include "thirdpersonfollower.h"
third_person_follower_t::third_person_follower_t(const point3f& target, float normalDistance) {
this->normalDistance = normalDistance;
}
void third_person_follower_t::lookAt(const point3f& target, float h, float v) {
point3f camera = target + getCamera(h, v);
gluLookAt(camera.x, camera.y, camera.z,
target.x, target.y, target.z,
0, 0, 1);
}
void third_person_follower_t::lookAtDebug() {
}
void third_person_follower_t::setMousePressingPosition(int x, int y) {
previousMousePositionX = x;
previousMousePositionY = y;
}
/**
* Called when the mouse were moved. The camera rotates around the target
* with horizontal and vertical movememnts.
*/
void third_person_follower_t::mouseDragged(int x, int y) {
int dx = x - previousMousePositionX;
int dy = y - previousMousePositionY;
move(-dx, dy);
previousMousePositionX = x;
previousMousePositionY = y;
}
void third_person_follower_t::move(float dx, float dy) {
setAngle(
horizontal + dx * horizontalFactor,
vertical + dy * verticalFactor
);
}
void third_person_follower_t::setAngle(float horizontal, float vertical) {
setAngle(horizontal, vertical, normalDistance);
}
// FIXME Why does the vertical angle doesn't work normally and need to be inverted?
void third_person_follower_t::setAngle(float horizontal, float vertical,
float distance) {
// do this for some reason I don't know why
// vertical = -vertical;
horizontal = clampAngle(horizontal);
// clamp the vertical angle to -60° and 60°
if (vertical < -maxVerticalAngle) {
vertical = -maxVerticalAngle;
} else if (vertical > maxVerticalAngle) {
vertical = maxVerticalAngle;
}
this->horizontal = horizontal;
this->vertical = vertical;
normalDistance = distance;
}
vector3f third_person_follower_t::getCamera(float h, float v) const {
// rotate <1,0,0> by vertical around the y axis
float r, z;
sincosf(vertical + v, &z, &r);
// rotate <x, 0, z> by horizontal around the z axis
// The z axis continues to be the same.
// printf("h = %f, v = %f\n", horizontal, vertical);
// g is from Ground
float x, y;
sincosf(horizontal + h, &y, &x);
return vector3f(r * x, r * y, z) * normalDistance;
}
void third_person_follower_t::follow(float dt) {
}
void third_person_follower_t::rotate(vector3f& v) const {
v.rotateY(vertical);
v.rotateZ(horizontal);
}