Khaotic Engine Reborn
Loading...
Searching...
No Matches
position_class.cpp
1#include "position_class.h"
2
3position_class::position_class()
4{
5 m_frameTime = 0.0f;
6 m_rotationY = 0.0f;
7 m_rotationX = 0.0f;
8 m_positionX = 0.0f;
9 m_positionY = 0.0f;
10 m_positionZ = 0.0f;
11 m_leftTurnSpeed = 0.0f;
12 m_rightTurnSpeed = 0.0f;
13 m_horizontalTurnSpeed = 0.0f;
14 m_verticalTurnSpeed = 0.0f;
15 m_cameraSpeed = 4.0f;
16 m_speed = m_cameraSpeed;
17}
18
19
20position_class::position_class(const position_class& other)
21{
22}
23
24
25position_class::~position_class()
26{
27}
28
29void position_class::SetFrameTime(float time)
30{
31 m_frameTime = time;
32 return;
33}
34
35void position_class::GetRotation(float& y, float& x) const
36{
37 y = m_rotationY;
38 x = m_rotationX;
39 return;
40}
41
42void position_class::GetPosition(float& x, float& y, float& z) const
43{
44 x = m_positionX;
45 y = m_positionY;
46 z = m_positionZ;
47 return;
48}
49
50void position_class::TurnLeft(bool keydown)
51{
52 // If the key is pressed increase the speed at which the camera turns left. If not slow down the turn speed.
53 if (keydown)
54 {
55 m_leftTurnSpeed += m_frameTime * 1.5f;
56
57 if (m_leftTurnSpeed > (m_frameTime * 200.0f))
58 {
59 m_leftTurnSpeed = m_frameTime * 200.0f;
60 }
61 }
62 else
63 {
64 m_leftTurnSpeed -= m_frameTime * 1.0f;
65
66 if (m_leftTurnSpeed < 0.0f)
67 {
68 m_leftTurnSpeed = 0.0f;
69 }
70 }
71
72 // Update the rotation using the turning speed.
73 m_rotationY -= m_leftTurnSpeed;
74 if (m_rotationY < 0.0f)
75 {
76 m_rotationY += 360.0f;
77 }
78
79 return;
80}
81
82
83void position_class::TurnRight(bool keydown)
84{
85 // If the key is pressed increase the speed at which the camera turns right. If not slow down the turn speed.
86 if (keydown)
87 {
88 m_rightTurnSpeed += m_frameTime * 1.5f;
89
90 if (m_rightTurnSpeed > (m_frameTime * 200.0f))
91 {
92 m_rightTurnSpeed = m_frameTime * 200.0f;
93 }
94 }
95 else
96 {
97 m_rightTurnSpeed -= m_frameTime * 1.0f;
98
99 if (m_rightTurnSpeed < 0.0f)
100 {
101 m_rightTurnSpeed = 0.0f;
102 }
103 }
104
105 // Update the rotation using the turning speed.
106 m_rotationY += m_rightTurnSpeed;
107 if (m_rotationY > 360.0f)
108 {
109 m_rotationY -= 360.0f;
110 }
111
112 return;
113}
114
115void position_class::TurnMouse(float deltaX, float deltaY, float sensitivity, bool rightMouseDown)
116{
117 // The turning speed is proportional to the horizontal mouse movement
118 m_horizontalTurnSpeed = deltaX * sensitivity;
119
120 if (rightMouseDown)
121 {
122 // Update the rotation using the turning speed
123 m_rotationY += m_horizontalTurnSpeed;
124 if (m_rotationY < 0.0f)
125 {
126 m_rotationY += 360.0f;
127 }
128 else if (m_rotationY > 360.0f)
129 {
130 m_rotationY -= 360.0f;
131 }
132
133 // The turning speed is proportional to the vertical mouse movement
134 m_verticalTurnSpeed = deltaY * sensitivity;
135
136 // Update the rotation using the turning speed
137 m_rotationX += m_verticalTurnSpeed;
138 if (m_rotationX < -90.0f)
139 {
140 m_rotationX = -90.0f;
141 }
142 else if (m_rotationX > 90.0f)
143 {
144 m_rotationX = 90.0f;
145 }
146 }
147 return;
148}
149
150void position_class::MoveCamera(bool forward, bool backward, bool left, bool right, bool up, bool down, bool scrollUp, bool scrollDown, bool rightClick)
151{
152 float radiansY, radiansX, speed;
153
154 // Set the speed of the camera if the right click is down.
155 if (scrollUp && rightClick)
156 {
157 m_cameraSpeed *= 1.1f;
158 }
159 if (scrollDown && rightClick)
160 {
161 m_cameraSpeed *= 0.9f;
162
163 if (m_cameraSpeed < 0.25f) // Minimum speed.
164 {
165 m_cameraSpeed = 0.25f;
166 }
167 }
168
169 // Convert degrees to radians.
170 radiansY = m_rotationY * 0.0174532925f;
171 radiansX = m_rotationX * 0.0174532925f;
172
174 // Update the position. //
176
177 // Moves the camera forward on a greater scale than the arrows.
178 if (scrollUp && !rightClick)
179 {
180 speed = m_speed * 20 * m_frameTime;
181 m_positionX += sinf(radiansY) * cosf(radiansX) * speed;
182 m_positionZ += cosf(radiansY) * cosf(radiansX) * speed;
183 m_positionY -= sinf(radiansX) * speed;
184 }
185
186 // Moves the camera backward on a greater scale than the arrows.
187 if (scrollDown && !rightClick)
188 {
189 speed = m_speed * 20 * m_frameTime;
190 m_positionX -= sinf(radiansY) * cosf(radiansX) * speed;
191 m_positionZ -= cosf(radiansY) * cosf(radiansX) * speed;
192 m_positionY += sinf(radiansX) * speed;
193 }
194
195 // Set the speed of the camera.
196 speed = m_cameraSpeed * m_frameTime;
197
198 // If moving forward, the position moves in the direction of the camera and accordingly to its angle.
199 if (forward)
200 {
201 m_positionX += sinf(radiansY) * cosf(radiansX) * speed;
202 m_positionZ += cosf(radiansY) * cosf(radiansX) * speed;
203 m_positionY -= sinf(radiansX) * speed;
204 }
205
206 // If moving backward, the position moves in the opposite direction of the camera and accordingly to its angle.
207 if (backward)
208 {
209 m_positionX -= sinf(radiansY) * cosf(radiansX) * speed;
210 m_positionZ -= cosf(radiansY) * cosf(radiansX) * speed;
211 m_positionY += sinf(radiansX) * speed;
212 }
213
214 // If moving left, the position moves to the left of the camera and accordingly to its angle.
215 if (left)
216 {
217 m_positionX -= cosf(radiansY) * speed;
218 m_positionZ += sinf(radiansY) * speed;
219 }
220
221 // If moving right, the position moves to the right of the camera and accordingly to its angle.
222 if (right)
223 {
224 m_positionX += cosf(radiansY) * speed;
225 m_positionZ -= sinf(radiansY) * speed;
226 }
227
228 // If moving up, the position moves up.
229 if (up)
230 {
231 m_positionY += speed;
232 }
233
234 // If moving down, the position moves down.
235 if (down)
236 {
237 m_positionY -= speed;
238 }
239
240 return;
241}