-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
134 lines (114 loc) · 2.68 KB
/
Camera.cpp
File metadata and controls
134 lines (114 loc) · 2.68 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
#include "Camera.h"
#include "Weapon.h"
#include <sstream>
#include "CollisionDetection.h"
#include "Utils.h"
Camera::Camera(GameEngine* engine) : Object(engine)
{
m_subject = new Vector3(0.0f, 28.0f, 3.0f);
setPosition(0.0f, 28.0f, 500.0f);
m_pitch = 0.0f;
m_yaw = 0.0f;
m_bobValue = 0.0f;
}
const char* Camera::getType()
{
return "Camera";
}
void Camera::setSubject(GLfloat x, GLfloat y, GLfloat z)
{
m_subject->x = x;
m_subject->y = y;
m_subject->z = z;
}
void Camera::adjustPitch(GLfloat angle)
{
m_pitch += angle;
if (m_pitch > 90.0f)
m_pitch = 90.0f;
else if (m_pitch < -90.0f)
m_pitch = -90.0f;
}
void Camera::adjustYaw(GLfloat angle)
{
m_yaw += angle;
if (m_yaw >= 360.0f)
m_yaw -= 360.0f;
else if (m_yaw < 0.0f)
m_yaw += 360.0f;
}
void Camera::resetYaw()
{
m_yaw = 0.0f;
}
void Camera::resetPitch()
{
m_pitch = 0.0f;
}
void Camera::moveForward()
{
adjustBob(true);
Vector3 newPos = getPosition() + getSubjectRelative();
setPosition(newPos.x, getPosition().y, newPos.z);
reposition();
}
void Camera::moveBackward()
{
adjustBob(false);
Vector3 newPos = getPosition() - getSubjectRelative();
setPosition(newPos.x, getPosition().y, newPos.z);
reposition();
}
void Camera::strafeLeft(GLfloat step)
{
Vector3 pos = getPosition();
pos.x += cosf(Utils::degreesToRadians(-m_yaw));
pos.z += sinf(Utils::degreesToRadians(-m_yaw));
setPosition(pos);
reposition();
}
void Camera::strafeRight(GLfloat step)
{
Vector3 pos = getPosition();
pos.x -= cosf(Utils::degreesToRadians(-m_yaw));
pos.z -= sinf(Utils::degreesToRadians(-m_yaw));
setPosition(pos);
reposition();
}
void Camera::adjustBob(bool forward)
{
if (forward)
{
m_bobValue += m_bobUp ? 0.07f : -0.07f;
}
else
{
m_bobValue += m_bobUp ? -0.07f : 0.07f;
}
if (m_bobValue >= 0.42f || m_bobValue <= -0.42f)
m_bobUp = !m_bobUp;
}
Vector3 Camera::getSubject()
{
return *m_subject;
}
Vector3 Camera::getSubjectRelative()
{
return *m_subject - *m_position;
}
void Camera::reposition()
{
GLfloat x = sinf(Utils::degreesToRadians(m_yaw));
GLfloat y = sinf(Utils::degreesToRadians(m_pitch));
GLfloat z = cosf(Utils::degreesToRadians(m_yaw));
Vector3 subjectLocal(x, y, z);
Vector3 subjectWorld = getPosition() + subjectLocal;
/* std::stringstream str;
str << "Local X: " << x << " Local Y: " << y << " Local Z: " << z << std::endl;
str << "World X: " << subjectWorld.x << " World Y: " << subjectWorld.y << " World Z: " << subjectWorld.z << std::endl;
OutputDebugString(str.str().c_str());*/
setSubject(subjectWorld.x, subjectWorld.y, subjectWorld.z);
}
Camera::~Camera(void)
{
}