-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
191 lines (148 loc) · 5.27 KB
/
Camera.cpp
File metadata and controls
191 lines (148 loc) · 5.27 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
#include "Camera.h"
// class based on www.gametutorials.com - see license.txt
Camera::Camera(){
init();
}
void Camera::init(){
position = CVector3(0.0f, 10.0f, -20.0f);
view = CVector3(0.0f, 10.0f, -19.0f);
upVector = CVector3(0.0f, 1.0f, 0.0f);
}
Camera::~Camera(){
}
void Camera::rotateView(float angle, float x, float y, float z){
CVector3 direction = getDirectionNormalized();
// mouse/touch look sensitivity
angle *= Engine::sensitivity;
const float cosTheta = cosf(angle);
const float sinTheta = sinf(angle);
CVector3 newView;
newView.x = (cosTheta + (1 - cosTheta) * x * x) * direction.x;
newView.x += ((1 - cosTheta) * x * y - z * sinTheta) * direction.y;
newView.x += ((1 - cosTheta) * x * z + y * sinTheta) * direction.z;
newView.y = ((1 - cosTheta) * x * y + z * sinTheta) * direction.x;
newView.y += (cosTheta + (1 - cosTheta) * y * y) * direction.y;
newView.y += ((1 - cosTheta) * y * z - x * sinTheta) * direction.z;
newView.z = ((1 - cosTheta) * x * z - y * sinTheta) * direction.x;
newView.z += ((1 - cosTheta) * y * z + x * sinTheta) * direction.y;
newView.z += (cosTheta + (1 - cosTheta) * z * z) * direction.z;
view = position + newView;
}
void Camera::strafeCamera(float speed){
speed *= -1.0f; // invert
position.x += strafe.x * speed;
position.z += strafe.z * speed;
view.x += strafe.x * speed;
view.z += strafe.z * speed;
}
void Camera::moveCamera(float speed){
CVector3 direction = getDirectionNormalized();
position += direction * speed;
view += direction * speed;
}
void Camera::applyKeyboardMovements(){
const float speedUp = 30.0f;
float speed = speedUp * Engine::frameInterval;
if(speed < 0.0f)
speed = -speed;
//#ifdef ANDROID
if(Engine::buttonPressed != CVector2(-666.6f, -666.6f)){
Engine::upKey = false;
Engine::downKey = false;
Engine::leftKey = false;
Engine::rightKey = false;
Engine::jumpKey = false;
Engine::crouchKey = false;
CVector2 translatedButtonPressed = CVector2(Engine::buttonPressed.x - ((float)Engine::fontSize * 1.32f), Engine::screenHeightGL - Engine::buttonPressed.y - ((float)Engine::fontSize * (4.0f / 8.0f)));
CVector2 distanceToLeftButton = Engine::buttonLeftCenter - translatedButtonPressed;
float distanceLeft = distanceToLeftButton.magnitude();
const float radiusForButton = 125.0f;
if(distanceLeft < radiusForButton && distanceLeft > 1.0f){
distanceToLeftButton.normalize();
int maxComponent = distanceToLeftButton.maxComponentAbs();
if(maxComponent == CVector2::X_COMPONENT){
if(distanceToLeftButton.x > 0.0f){
Engine::rightKey = true; // swapped left/right on purpose!
} else {
Engine::leftKey = true;
}
} else {
if(distanceToLeftButton.y > 0.0f){
Engine::downKey = true;
} else {
Engine::upKey = true;
}
}
}
// Handle right button field for mouse view
CVector2 distanceToRightButton = Engine::buttonRightCenter - translatedButtonPressed;
float distanceRight = distanceToRightButton.magnitude();
if(distanceRight < radiusForButton){
float angleY = 0.0f;
float angleZ = 0.0f;
static float currentRotX = 0.0f;
// mouse look
CVector2 mouseMotion = CVector2(-distanceToRightButton.x, distanceToRightButton.y);
angleY = (float)( (-mouseMotion.x) / 3500.0f);
angleZ = (float)( (-mouseMotion.y) / 3500.0f);
CVector3 viewMinusPosition = view - position;
float dotProduct = upVector.dotProduct(&viewMinusPosition);
currentRotX -= angleZ;
if(currentRotX > 1.0f)
currentRotX = 1.0f;
else if(currentRotX < -1.0f)
currentRotX = -1.0f;
if(dotProduct > 0.95f){
viewMinusPosition.x += 0.05f;
viewMinusPosition.y -= 0.1f;
viewMinusPosition.normalize();
dotProduct = upVector.dotProduct(&viewMinusPosition);
}
if(dotProduct < -0.95f){
viewMinusPosition.x += 0.05f;
viewMinusPosition.y += 0.1f;
viewMinusPosition.normalize();
dotProduct = upVector.dotProduct(&viewMinusPosition);
}
CVector3 axis;
axis = axis.cross(viewMinusPosition, upVector);
axis.normalize();
rotateView(angleZ, axis.x, axis.y, axis.z);
rotateView(angleY, 0, 1, 0);
}
}
//#endif
if(Engine::crouchKey){
position.y -= speed;
view.y -= speed;
}
if(Engine::jumpKey){
position.y += speed;
view.y += speed;
}
if(Engine::upKey){
moveCamera(speed);
}
if(Engine::downKey){
moveCamera(-speed);
}
if(Engine::leftKey){
strafeCamera(-speed);
}
if(Engine::rightKey){
strafeCamera(speed);
}
}
void Camera::update() {
strafe = strafe.cross(view - position, upVector);
strafe.normalize();
applyKeyboardMovements();
}
CVector3 Camera::getDirectionNormalized(){
CVector3 dir = view - position;
if(dir.magnitude() < 0.01f) {
cerr << "Camera direction is too small..." << endl;
}
dir.normalize();
return dir;
}