1 | /******************************************************************************************* |
2 | * |
3 | * raylib.camera - Camera system with multiple modes support |
4 | * |
5 | * NOTE: Memory footprint of this library is aproximately 52 bytes (global variables) |
6 | * |
7 | * CONFIGURATION: |
8 | * |
9 | * #define CAMERA_IMPLEMENTATION |
10 | * Generates the implementation of the library into the included file. |
11 | * If not defined, the library is in header only mode and can be included in other headers |
12 | * or source files without problems. But only ONE file should hold the implementation. |
13 | * |
14 | * #define CAMERA_STANDALONE |
15 | * If defined, the library can be used as standalone as a camera system but some |
16 | * functions must be redefined to manage inputs accordingly. |
17 | * |
18 | * CONTRIBUTORS: |
19 | * Ramon Santamaria: Supervision, review, update and maintenance |
20 | * Marc Palau: Initial implementation (2014) |
21 | * |
22 | * |
23 | * LICENSE: zlib/libpng |
24 | * |
25 | * Copyright (c) 2015-2020 Ramon Santamaria (@raysan5) |
26 | * |
27 | * This software is provided "as-is", without any express or implied warranty. In no event |
28 | * will the authors be held liable for any damages arising from the use of this software. |
29 | * |
30 | * Permission is granted to anyone to use this software for any purpose, including commercial |
31 | * applications, and to alter it and redistribute it freely, subject to the following restrictions: |
32 | * |
33 | * 1. The origin of this software must not be misrepresented; you must not claim that you |
34 | * wrote the original software. If you use this software in a product, an acknowledgment |
35 | * in the product documentation would be appreciated but is not required. |
36 | * |
37 | * 2. Altered source versions must be plainly marked as such, and must not be misrepresented |
38 | * as being the original software. |
39 | * |
40 | * 3. This notice may not be removed or altered from any source distribution. |
41 | * |
42 | **********************************************************************************************/ |
43 | |
44 | #ifndef CAMERA_H |
45 | #define CAMERA_H |
46 | |
47 | //---------------------------------------------------------------------------------- |
48 | // Defines and Macros |
49 | //---------------------------------------------------------------------------------- |
50 | //... |
51 | |
52 | //---------------------------------------------------------------------------------- |
53 | // Types and Structures Definition |
54 | // NOTE: Below types are required for CAMERA_STANDALONE usage |
55 | //---------------------------------------------------------------------------------- |
56 | #if defined(CAMERA_STANDALONE) |
57 | // Vector2 type |
58 | typedef struct Vector2 { |
59 | float x; |
60 | float y; |
61 | } Vector2; |
62 | |
63 | // Vector3 type |
64 | typedef struct Vector3 { |
65 | float x; |
66 | float y; |
67 | float z; |
68 | } Vector3; |
69 | |
70 | // Camera type, defines a camera position/orientation in 3d space |
71 | typedef struct Camera3D { |
72 | Vector3 position; // Camera position |
73 | Vector3 target; // Camera target it looks-at |
74 | Vector3 up; // Camera up vector (rotation over its axis) |
75 | float fovy; // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic |
76 | int type; // Camera type, defines projection type: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC |
77 | } Camera3D; |
78 | |
79 | typedef Camera3D Camera; // Camera type fallback, defaults to Camera3D |
80 | |
81 | // Camera system modes |
82 | typedef enum { |
83 | CAMERA_CUSTOM = 0, |
84 | CAMERA_FREE, |
85 | CAMERA_ORBITAL, |
86 | CAMERA_FIRST_PERSON, |
87 | CAMERA_THIRD_PERSON |
88 | } CameraMode; |
89 | |
90 | // Camera projection modes |
91 | typedef enum { |
92 | CAMERA_PERSPECTIVE = 0, |
93 | CAMERA_ORTHOGRAPHIC |
94 | } CameraType; |
95 | #endif |
96 | |
97 | #ifdef __cplusplus |
98 | extern "C" { // Prevents name mangling of functions |
99 | #endif |
100 | |
101 | //---------------------------------------------------------------------------------- |
102 | // Global Variables Definition |
103 | //---------------------------------------------------------------------------------- |
104 | //... |
105 | |
106 | //---------------------------------------------------------------------------------- |
107 | // Module Functions Declaration |
108 | //---------------------------------------------------------------------------------- |
109 | #if defined(CAMERA_STANDALONE) |
110 | void SetCameraMode(Camera camera, int mode); // Set camera mode (multiple camera modes available) |
111 | void UpdateCamera(Camera *camera); // Update camera position for selected mode |
112 | |
113 | void SetCameraPanControl(int panKey); // Set camera pan key to combine with mouse movement (free camera) |
114 | void SetCameraAltControl(int altKey); // Set camera alt key to combine with mouse movement (free camera) |
115 | void SetCameraSmoothZoomControl(int szoomKey); // Set camera smooth zoom key to combine with mouse (free camera) |
116 | void SetCameraMoveControls(int frontKey, int backKey, |
117 | int rightKey, int leftKey, |
118 | int upKey, int downKey); // Set camera move controls (1st person and 3rd person cameras) |
119 | #endif |
120 | |
121 | #ifdef __cplusplus |
122 | } |
123 | #endif |
124 | |
125 | #endif // CAMERA_H |
126 | |
127 | |
128 | /*********************************************************************************** |
129 | * |
130 | * CAMERA IMPLEMENTATION |
131 | * |
132 | ************************************************************************************/ |
133 | |
134 | #if defined(CAMERA_IMPLEMENTATION) |
135 | |
136 | #include <math.h> // Required for: sinf(), cosf(), sqrtf() |
137 | |
138 | #ifndef PI |
139 | #define PI 3.14159265358979323846 |
140 | #endif |
141 | #ifndef DEG2RAD |
142 | #define DEG2RAD (PI/180.0f) |
143 | #endif |
144 | #ifndef RAD2DEG |
145 | #define RAD2DEG (180.0f/PI) |
146 | #endif |
147 | |
148 | //---------------------------------------------------------------------------------- |
149 | // Defines and Macros |
150 | //---------------------------------------------------------------------------------- |
151 | // Camera mouse movement sensitivity |
152 | #define CAMERA_MOUSE_MOVE_SENSITIVITY 0.003f |
153 | #define CAMERA_MOUSE_SCROLL_SENSITIVITY 1.5f |
154 | |
155 | // FREE_CAMERA |
156 | #define CAMERA_FREE_MOUSE_SENSITIVITY 0.01f |
157 | #define CAMERA_FREE_DISTANCE_MIN_CLAMP 0.3f |
158 | #define CAMERA_FREE_DISTANCE_MAX_CLAMP 120.0f |
159 | #define CAMERA_FREE_MIN_CLAMP 85.0f |
160 | #define CAMERA_FREE_MAX_CLAMP -85.0f |
161 | #define CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY 0.05f |
162 | #define CAMERA_FREE_PANNING_DIVIDER 5.1f |
163 | |
164 | // ORBITAL_CAMERA |
165 | #define CAMERA_ORBITAL_SPEED 0.01f // Radians per frame |
166 | |
167 | // FIRST_PERSON |
168 | //#define CAMERA_FIRST_PERSON_MOUSE_SENSITIVITY 0.003f |
169 | #define CAMERA_FIRST_PERSON_FOCUS_DISTANCE 25.0f |
170 | #define CAMERA_FIRST_PERSON_MIN_CLAMP 89.0f |
171 | #define CAMERA_FIRST_PERSON_MAX_CLAMP -89.0f |
172 | |
173 | #define CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER 5.0f |
174 | #define CAMERA_FIRST_PERSON_STEP_DIVIDER 30.0f |
175 | #define CAMERA_FIRST_PERSON_WAVING_DIVIDER 200.0f |
176 | |
177 | // THIRD_PERSON |
178 | //#define CAMERA_THIRD_PERSON_MOUSE_SENSITIVITY 0.003f |
179 | #define CAMERA_THIRD_PERSON_DISTANCE_CLAMP 1.2f |
180 | #define CAMERA_THIRD_PERSON_MIN_CLAMP 5.0f |
181 | #define CAMERA_THIRD_PERSON_MAX_CLAMP -85.0f |
182 | #define CAMERA_THIRD_PERSON_OFFSET (Vector3){ 0.4f, 0.0f, 0.0f } |
183 | |
184 | // PLAYER (used by camera) |
185 | #define PLAYER_MOVEMENT_SENSITIVITY 20.0f |
186 | |
187 | //---------------------------------------------------------------------------------- |
188 | // Types and Structures Definition |
189 | //---------------------------------------------------------------------------------- |
190 | // Camera move modes (first person and third person cameras) |
191 | typedef enum { |
192 | MOVE_FRONT = 0, |
193 | MOVE_BACK, |
194 | MOVE_RIGHT, |
195 | MOVE_LEFT, |
196 | MOVE_UP, |
197 | MOVE_DOWN |
198 | } CameraMove; |
199 | |
200 | // Camera global state context data |
201 | typedef struct { |
202 | int mode; // Current camera mode |
203 | float targetDistance; // Camera distance from position to target |
204 | float playerEyesPosition; // Default player eyes position from ground (in meters) |
205 | Vector2 angle; // Camera angle in plane XZ |
206 | |
207 | int moveControl[6]; |
208 | int smoothZoomControl; // raylib: KEY_LEFT_CONTROL |
209 | int altControl; // raylib: KEY_LEFT_ALT |
210 | int panControl; // raylib: MOUSE_MIDDLE_BUTTON |
211 | } CameraData; |
212 | |
213 | //---------------------------------------------------------------------------------- |
214 | // Global Variables Definition |
215 | //---------------------------------------------------------------------------------- |
216 | static CameraData CAMERA = { // Global CAMERA state context |
217 | .mode = 0, |
218 | .targetDistance = 0, |
219 | .playerEyesPosition = 1.85f, |
220 | .angle = { 0 }, |
221 | .moveControl = { 'W', 'S', 'D', 'A', 'E', 'Q' }, |
222 | .smoothZoomControl = 341, |
223 | .altControl = 342, |
224 | .panControl = 2 |
225 | }; |
226 | |
227 | //---------------------------------------------------------------------------------- |
228 | // Module specific Functions Declaration |
229 | //---------------------------------------------------------------------------------- |
230 | #if defined(CAMERA_STANDALONE) |
231 | // NOTE: Camera controls depend on some raylib input functions |
232 | static void EnableCursor() {} // Unlock cursor |
233 | static void DisableCursor() {} // Lock cursor |
234 | |
235 | static int IsKeyDown(int key) { return 0; } |
236 | |
237 | static int IsMouseButtonDown(int button) { return 0;} |
238 | static int GetMouseWheelMove() { return 0; } |
239 | static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; } |
240 | #endif |
241 | |
242 | //---------------------------------------------------------------------------------- |
243 | // Module Functions Definition |
244 | //---------------------------------------------------------------------------------- |
245 | |
246 | // Select camera mode (multiple camera modes available) |
247 | void SetCameraMode(Camera camera, int mode) |
248 | { |
249 | Vector3 v1 = camera.position; |
250 | Vector3 v2 = camera.target; |
251 | |
252 | float dx = v2.x - v1.x; |
253 | float dy = v2.y - v1.y; |
254 | float dz = v2.z - v1.z; |
255 | |
256 | CAMERA.targetDistance = sqrtf(dx*dx + dy*dy + dz*dz); |
257 | |
258 | // Camera angle calculation |
259 | CAMERA.angle.x = atan2f(dx, dz); // Camera angle in plane XZ (0 aligned with Z, move positive CCW) |
260 | CAMERA.angle.y = atan2f(dy, sqrtf(dx*dx + dz*dz)); // Camera angle in plane XY (0 aligned with X, move positive CW) |
261 | |
262 | CAMERA.playerEyesPosition = camera.position.y; |
263 | |
264 | // Lock cursor for first person and third person cameras |
265 | if ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON)) DisableCursor(); |
266 | else EnableCursor(); |
267 | |
268 | CAMERA.mode = mode; |
269 | } |
270 | |
271 | // Update camera depending on selected mode |
272 | // NOTE: Camera controls depend on some raylib functions: |
273 | // System: EnableCursor(), DisableCursor() |
274 | // Mouse: IsMouseButtonDown(), GetMousePosition(), GetMouseWheelMove() |
275 | // Keys: IsKeyDown() |
276 | // TODO: Port to quaternion-based camera (?) |
277 | void UpdateCamera(Camera *camera) |
278 | { |
279 | static int swingCounter = 0; // Used for 1st person swinging movement |
280 | static Vector2 previousMousePosition = { 0.0f, 0.0f }; |
281 | |
282 | // TODO: Compute CAMERA.targetDistance and CAMERA.angle here (?) |
283 | |
284 | // Mouse movement detection |
285 | Vector2 mousePositionDelta = { 0.0f, 0.0f }; |
286 | Vector2 mousePosition = GetMousePosition(); |
287 | int mouseWheelMove = GetMouseWheelMove(); |
288 | |
289 | // Keys input detection |
290 | bool panKey = IsMouseButtonDown(CAMERA.panControl); |
291 | bool altKey = IsKeyDown(CAMERA.altControl); |
292 | bool szoomKey = IsKeyDown(CAMERA.smoothZoomControl); |
293 | bool direction[6] = { IsKeyDown(CAMERA.moveControl[MOVE_FRONT]), |
294 | IsKeyDown(CAMERA.moveControl[MOVE_BACK]), |
295 | IsKeyDown(CAMERA.moveControl[MOVE_RIGHT]), |
296 | IsKeyDown(CAMERA.moveControl[MOVE_LEFT]), |
297 | IsKeyDown(CAMERA.moveControl[MOVE_UP]), |
298 | IsKeyDown(CAMERA.moveControl[MOVE_DOWN]) }; |
299 | |
300 | // TODO: Touch input detection (probably gestures system required) |
301 | |
302 | if (CAMERA.mode != CAMERA_CUSTOM) |
303 | { |
304 | mousePositionDelta.x = mousePosition.x - previousMousePosition.x; |
305 | mousePositionDelta.y = mousePosition.y - previousMousePosition.y; |
306 | |
307 | previousMousePosition = mousePosition; |
308 | } |
309 | |
310 | // Support for multiple automatic camera modes |
311 | // NOTE: In case of CAMERA_CUSTOM nothing happens here, user must update it manually |
312 | switch (CAMERA.mode) |
313 | { |
314 | case CAMERA_FREE: // Camera free controls, using standard 3d-content-creation scheme |
315 | { |
316 | // Camera zoom |
317 | if ((CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0)) |
318 | { |
319 | CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); |
320 | if (CAMERA.targetDistance > CAMERA_FREE_DISTANCE_MAX_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MAX_CLAMP; |
321 | } |
322 | |
323 | // Camera looking down |
324 | // TODO: Review, weird comparison of CAMERA.targetDistance == 120.0f? |
325 | else if ((camera->position.y > camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0)) |
326 | { |
327 | camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
328 | camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
329 | camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
330 | } |
331 | else if ((camera->position.y > camera->target.y) && (camera->target.y >= 0)) |
332 | { |
333 | camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
334 | camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
335 | camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
336 | |
337 | // if (camera->target.y < 0) camera->target.y = -0.001; |
338 | } |
339 | else if ((camera->position.y > camera->target.y) && (camera->target.y < 0) && (mouseWheelMove > 0)) |
340 | { |
341 | CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); |
342 | if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP; |
343 | } |
344 | // Camera looking up |
345 | // TODO: Review, weird comparisson of CAMERA.targetDistance == 120.0f? |
346 | else if ((camera->position.y < camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0)) |
347 | { |
348 | camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
349 | camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
350 | camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
351 | } |
352 | else if ((camera->position.y < camera->target.y) && (camera->target.y <= 0)) |
353 | { |
354 | camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
355 | camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
356 | camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance; |
357 | |
358 | // if (camera->target.y > 0) camera->target.y = 0.001; |
359 | } |
360 | else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0)) |
361 | { |
362 | CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); |
363 | if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP; |
364 | } |
365 | |
366 | // Input keys checks |
367 | if (panKey) |
368 | { |
369 | if (altKey) // Alternative key behaviour |
370 | { |
371 | if (szoomKey) |
372 | { |
373 | // Camera smooth zoom |
374 | CAMERA.targetDistance += (mousePositionDelta.y*CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY); |
375 | } |
376 | else |
377 | { |
378 | // Camera rotation |
379 | CAMERA.angle.x += mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY; |
380 | CAMERA.angle.y += mousePositionDelta.y*-CAMERA_FREE_MOUSE_SENSITIVITY; |
381 | |
382 | // Angle clamp |
383 | if (CAMERA.angle.y > CAMERA_FREE_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MIN_CLAMP*DEG2RAD; |
384 | else if (CAMERA.angle.y < CAMERA_FREE_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MAX_CLAMP*DEG2RAD; |
385 | } |
386 | } |
387 | else |
388 | { |
389 | // Camera panning |
390 | camera->target.x += ((mousePositionDelta.x*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.x) + (mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*sinf(CAMERA.angle.x)*sinf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER); |
391 | camera->target.y += ((mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER); |
392 | camera->target.z += ((mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY)*sinf(CAMERA.angle.x) + (mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.x)*sinf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER); |
393 | } |
394 | } |
395 | |
396 | // Update camera position with changes |
397 | camera->position.x = -sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x; |
398 | camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance + camera->target.y; |
399 | camera->position.z = -cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z; |
400 | |
401 | } break; |
402 | case CAMERA_ORBITAL: // Camera just orbits around target, only zoom allowed |
403 | { |
404 | CAMERA.angle.x += CAMERA_ORBITAL_SPEED; // Camera orbit angle |
405 | CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); // Camera zoom |
406 | |
407 | // Camera distance clamp |
408 | if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP; |
409 | |
410 | // Update camera position with changes |
411 | camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x; |
412 | camera->position.y = ((CAMERA.angle.y <= 0.0f)? 1 : -1)*sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y; |
413 | camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z; |
414 | |
415 | } break; |
416 | case CAMERA_FIRST_PERSON: // Camera moves as in a first-person game, controls are configurable |
417 | { |
418 | camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] - |
419 | sinf(CAMERA.angle.x)*direction[MOVE_FRONT] - |
420 | cosf(CAMERA.angle.x)*direction[MOVE_LEFT] + |
421 | cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY; |
422 | |
423 | camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] - |
424 | sinf(CAMERA.angle.y)*direction[MOVE_BACK] + |
425 | 1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY; |
426 | |
427 | camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] - |
428 | cosf(CAMERA.angle.x)*direction[MOVE_FRONT] + |
429 | sinf(CAMERA.angle.x)*direction[MOVE_LEFT] - |
430 | sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY; |
431 | |
432 | // Camera orientation calculation |
433 | CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY); |
434 | CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY); |
435 | |
436 | // Angle clamp |
437 | if (CAMERA.angle.y > CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD; |
438 | else if (CAMERA.angle.y < CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD; |
439 | |
440 | // Recalculate camera target considering translation and rotation |
441 | Matrix translation = MatrixTranslate(0, 0, (CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER)); |
442 | Matrix rotation = MatrixRotateXYZ((Vector3){ PI*2 - CAMERA.angle.y, PI*2 - CAMERA.angle.x, 0 }); |
443 | Matrix transform = MatrixMultiply(translation, rotation); |
444 | |
445 | camera->target.x = camera->position.x - transform.m12; |
446 | camera->target.y = camera->position.y - transform.m13; |
447 | camera->target.z = camera->position.z - transform.m14; |
448 | |
449 | // If movement detected (some key pressed), increase swinging |
450 | for (int i = 0; i < 6; i++) if (direction[i]) { swingCounter++; break; } |
451 | |
452 | // Camera position update |
453 | // NOTE: On CAMERA_FIRST_PERSON player Y-movement is limited to player 'eyes position' |
454 | camera->position.y = CAMERA.playerEyesPosition - sinf(swingCounter/CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER)/CAMERA_FIRST_PERSON_STEP_DIVIDER; |
455 | |
456 | camera->up.x = sinf(swingCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER; |
457 | camera->up.z = -sinf(swingCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER; |
458 | |
459 | } break; |
460 | case CAMERA_THIRD_PERSON: // Camera moves as in a third-person game, following target at a distance, controls are configurable |
461 | { |
462 | camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] - |
463 | sinf(CAMERA.angle.x)*direction[MOVE_FRONT] - |
464 | cosf(CAMERA.angle.x)*direction[MOVE_LEFT] + |
465 | cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY; |
466 | |
467 | camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] - |
468 | sinf(CAMERA.angle.y)*direction[MOVE_BACK] + |
469 | 1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY; |
470 | |
471 | camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] - |
472 | cosf(CAMERA.angle.x)*direction[MOVE_FRONT] + |
473 | sinf(CAMERA.angle.x)*direction[MOVE_LEFT] - |
474 | sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY; |
475 | |
476 | // Camera orientation calculation |
477 | CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY); |
478 | CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY); |
479 | |
480 | // Angle clamp |
481 | if (CAMERA.angle.y > CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD; |
482 | else if (CAMERA.angle.y < CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD; |
483 | |
484 | // Camera zoom |
485 | CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY); |
486 | |
487 | // Camera distance clamp |
488 | if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP; |
489 | |
490 | // TODO: It seems camera->position is not correctly updated or some rounding issue makes the camera move straight to camera->target... |
491 | camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x; |
492 | if (CAMERA.angle.y <= 0.0f) camera->position.y = sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y; |
493 | else camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y; |
494 | camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z; |
495 | |
496 | } break; |
497 | case CAMERA_CUSTOM: break; |
498 | default: break; |
499 | } |
500 | } |
501 | |
502 | // Set camera pan key to combine with mouse movement (free camera) |
503 | void SetCameraPanControl(int panKey) { CAMERA.panControl = panKey; } |
504 | |
505 | // Set camera alt key to combine with mouse movement (free camera) |
506 | void SetCameraAltControl(int altKey) { CAMERA.altControl = altKey; } |
507 | |
508 | // Set camera smooth zoom key to combine with mouse (free camera) |
509 | void SetCameraSmoothZoomControl(int szoomKey) { CAMERA.smoothZoomControl = szoomKey; } |
510 | |
511 | // Set camera move controls (1st person and 3rd person cameras) |
512 | void SetCameraMoveControls(int frontKey, int backKey, int rightKey, int leftKey, int upKey, int downKey) |
513 | { |
514 | CAMERA.moveControl[MOVE_FRONT] = frontKey; |
515 | CAMERA.moveControl[MOVE_BACK] = backKey; |
516 | CAMERA.moveControl[MOVE_RIGHT] = rightKey; |
517 | CAMERA.moveControl[MOVE_LEFT] = leftKey; |
518 | CAMERA.moveControl[MOVE_UP] = upKey; |
519 | CAMERA.moveControl[MOVE_DOWN] = downKey; |
520 | } |
521 | |
522 | #endif // CAMERA_IMPLEMENTATION |
523 | |