]> git.sesse.net Git - pistorm/blob - raylib/camera.h
Add Meson build files.
[pistorm] / raylib / camera.h
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-2021 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     } CameraProjection;
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 keyPan);                       // Set camera pan key to combine with mouse movement (free camera)
114 void SetCameraAltControl(int keyAlt);                       // 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 keyFront, int keyBack,
117                            int keyRight, int keyLeft,
118                            int keyUp, int keyDown);         // 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 //----------------------------------------------------------------------------------
139 // Defines and Macros
140 //----------------------------------------------------------------------------------
141 #ifndef PI
142     #define PI 3.14159265358979323846
143 #endif
144 #ifndef DEG2RAD
145     #define DEG2RAD (PI/180.0f)
146 #endif
147 #ifndef RAD2DEG
148     #define RAD2DEG (180.0f/PI)
149 #endif
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  8.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 [56 bytes]
201 typedef struct {
202     unsigned int mode;              // Current camera mode
203     float targetDistance;           // Camera distance from position to target
204     float playerEyesPosition;       // Player eyes position from ground (in meters)
205     Vector2 angle;                  // Camera angle in plane XZ
206
207     // Camera movement control keys
208     int moveControl[6];             // Move controls (CAMERA_FIRST_PERSON)
209     int smoothZoomControl;          // Smooth zoom control key
210     int altControl;                 // Alternative control key
211     int panControl;                 // Pan view control key
212 } CameraData;
213
214 //----------------------------------------------------------------------------------
215 // Global Variables Definition
216 //----------------------------------------------------------------------------------
217 static CameraData CAMERA = {        // Global CAMERA state context
218     .mode = 0,
219     .targetDistance = 0,
220     .playerEyesPosition = 1.85f,
221     .angle = { 0 },
222     .moveControl = { 'W', 'S', 'D', 'A', 'E', 'Q' },
223     .smoothZoomControl = 341,       // raylib: KEY_LEFT_CONTROL
224     .altControl = 342,              // raylib: KEY_LEFT_ALT
225     .panControl = 2                 // raylib: MOUSE_MIDDLE_BUTTON
226 };
227
228 //----------------------------------------------------------------------------------
229 // Module specific Functions Declaration
230 //----------------------------------------------------------------------------------
231 #if defined(CAMERA_STANDALONE)
232 // NOTE: Camera controls depend on some raylib input functions
233 static void EnableCursor() {}       // Unlock cursor
234 static void DisableCursor() {}      // Lock cursor
235
236 static int IsKeyDown(int key) { return 0; }
237
238 static int IsMouseButtonDown(int button) { return 0;}
239 static float GetMouseWheelMove() { return 0.0f; }
240 static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
241 #endif
242
243 //----------------------------------------------------------------------------------
244 // Module Functions Definition
245 //----------------------------------------------------------------------------------
246
247 // Select camera mode (multiple camera modes available)
248 void SetCameraMode(Camera camera, int mode)
249 {
250     Vector3 v1 = camera.position;
251     Vector3 v2 = camera.target;
252
253     float dx = v2.x - v1.x;
254     float dy = v2.y - v1.y;
255     float dz = v2.z - v1.z;
256
257     CAMERA.targetDistance = sqrtf(dx*dx + dy*dy + dz*dz);   // Distance to target
258
259     // Camera angle calculation
260     CAMERA.angle.x = atan2f(dx, dz);                        // Camera angle in plane XZ (0 aligned with Z, move positive CCW)
261     CAMERA.angle.y = atan2f(dy, sqrtf(dx*dx + dz*dz));      // Camera angle in plane XY (0 aligned with X, move positive CW)
262
263     CAMERA.playerEyesPosition = camera.position.y;          // Init player eyes position to camera Y position
264
265     // Lock cursor for first person and third person cameras
266     if ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON)) DisableCursor();
267     else EnableCursor();
268
269     CAMERA.mode = mode;
270 }
271
272 // Update camera depending on selected mode
273 // NOTE: Camera controls depend on some raylib functions:
274 //       System: EnableCursor(), DisableCursor()
275 //       Mouse: IsMouseButtonDown(), GetMousePosition(), GetMouseWheelMove()
276 //       Keys:  IsKeyDown()
277 // TODO: Port to quaternion-based camera (?)
278 void UpdateCamera(Camera *camera)
279 {
280     static int swingCounter = 0;    // Used for 1st person swinging movement
281     static Vector2 previousMousePosition = { 0.0f, 0.0f };
282
283     // TODO: Compute CAMERA.targetDistance and CAMERA.angle here (?)
284
285     // Mouse movement detection
286     Vector2 mousePositionDelta = { 0.0f, 0.0f };
287     Vector2 mousePosition = GetMousePosition();
288     float mouseWheelMove = GetMouseWheelMove();
289
290     // Keys input detection
291     // TODO: Input detection is raylib-dependant, it could be moved outside the module
292     bool keyPan = IsMouseButtonDown(CAMERA.panControl);
293     bool keyAlt = IsKeyDown(CAMERA.altControl);
294     bool szoomKey = IsKeyDown(CAMERA.smoothZoomControl);
295     bool direction[6] = { IsKeyDown(CAMERA.moveControl[MOVE_FRONT]),
296                           IsKeyDown(CAMERA.moveControl[MOVE_BACK]),
297                           IsKeyDown(CAMERA.moveControl[MOVE_RIGHT]),
298                           IsKeyDown(CAMERA.moveControl[MOVE_LEFT]),
299                           IsKeyDown(CAMERA.moveControl[MOVE_UP]),
300                           IsKeyDown(CAMERA.moveControl[MOVE_DOWN]) };
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             else if ((camera->position.y > camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
325             {
326                 camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
327                 camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
328                 camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
329             }
330             else if ((camera->position.y > camera->target.y) && (camera->target.y >= 0))
331             {
332                 camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
333                 camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
334                 camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
335
336                 // if (camera->target.y < 0) camera->target.y = -0.001;
337             }
338             else if ((camera->position.y > camera->target.y) && (camera->target.y < 0) && (mouseWheelMove > 0))
339             {
340                 CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
341                 if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
342             }
343             // Camera looking up
344             else if ((camera->position.y < camera->target.y) && (CAMERA.targetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
345             {
346                 camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
347                 camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
348                 camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
349             }
350             else if ((camera->position.y < camera->target.y) && (camera->target.y <= 0))
351             {
352                 camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
353                 camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
354                 camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_MOUSE_SCROLL_SENSITIVITY/CAMERA.targetDistance;
355
356                 // if (camera->target.y > 0) camera->target.y = 0.001;
357             }
358             else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0))
359             {
360                 CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
361                 if (CAMERA.targetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) CAMERA.targetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
362             }
363
364             // Input keys checks
365             if (keyPan)
366             {
367                 if (keyAlt)     // Alternative key behaviour
368                 {
369                     if (szoomKey)
370                     {
371                         // Camera smooth zoom
372                         CAMERA.targetDistance += (mousePositionDelta.y*CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY);
373                     }
374                     else
375                     {
376                         // Camera rotation
377                         CAMERA.angle.x += mousePositionDelta.x*-CAMERA_FREE_MOUSE_SENSITIVITY;
378                         CAMERA.angle.y += mousePositionDelta.y*-CAMERA_FREE_MOUSE_SENSITIVITY;
379
380                         // Angle clamp
381                         if (CAMERA.angle.y > CAMERA_FREE_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MIN_CLAMP*DEG2RAD;
382                         else if (CAMERA.angle.y < CAMERA_FREE_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FREE_MAX_CLAMP*DEG2RAD;
383                     }
384                 }
385                 else
386                 {
387                     // Camera panning
388                     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);
389                     camera->target.y += ((mousePositionDelta.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cosf(CAMERA.angle.y))*(CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER);
390                     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);
391                 }
392             }
393
394             // Update camera position with changes
395             camera->position.x = -sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
396             camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance + camera->target.y;
397             camera->position.z = -cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
398
399         } break;
400         case CAMERA_ORBITAL:        // Camera just orbits around target, only zoom allowed
401         {
402             CAMERA.angle.x += CAMERA_ORBITAL_SPEED;      // Camera orbit angle
403             CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);   // Camera zoom
404
405             // Camera distance clamp
406             if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;
407
408             // Update camera position with changes
409             camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
410             camera->position.y = ((CAMERA.angle.y <= 0.0f)? 1 : -1)*sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y;
411             camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
412
413         } break;
414         case CAMERA_FIRST_PERSON:   // Camera moves as in a first-person game, controls are configurable
415         {
416             camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
417                                    sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
418                                    cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
419                                    cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
420
421             camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
422                                    sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
423                                    1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY;
424
425             camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
426                                    cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
427                                    sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
428                                    sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
429
430             // Camera orientation calculation
431             CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
432             CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
433
434             // Angle clamp
435             if (CAMERA.angle.y > CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD;
436             else if (CAMERA.angle.y < CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD;
437
438             // Recalculate camera target considering translation and rotation
439             Matrix translation = MatrixTranslate(0, 0, (CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER));
440             Matrix rotation = MatrixRotateXYZ((Vector3){ PI*2 - CAMERA.angle.y, PI*2 - CAMERA.angle.x, 0 });
441             Matrix transform = MatrixMultiply(translation, rotation);
442
443             camera->target.x = camera->position.x - transform.m12;
444             camera->target.y = camera->position.y - transform.m13;
445             camera->target.z = camera->position.z - transform.m14;
446
447             // If movement detected (some key pressed), increase swinging
448             for (int i = 0; i < 6; i++) if (direction[i]) { swingCounter++; break; }
449
450             // Camera position update
451             // NOTE: On CAMERA_FIRST_PERSON player Y-movement is limited to player 'eyes position'
452             camera->position.y = CAMERA.playerEyesPosition - sinf(swingCounter/CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER)/CAMERA_FIRST_PERSON_STEP_DIVIDER;
453
454             camera->up.x = sinf(swingCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER;
455             camera->up.z = -sinf(swingCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER;
456
457         } break;
458         case CAMERA_THIRD_PERSON:   // Camera moves as in a third-person game, following target at a distance, controls are configurable
459         {
460             camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
461                                    sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
462                                    cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
463                                    cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
464
465             camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
466                                    sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
467                                    1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY;
468
469             camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
470                                    cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
471                                    sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
472                                    sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
473
474             // Camera orientation calculation
475             CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
476             CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
477
478             // Angle clamp
479             if (CAMERA.angle.y > CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD;
480             else if (CAMERA.angle.y < CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD;
481
482             // Camera zoom
483             CAMERA.targetDistance -= (mouseWheelMove*CAMERA_MOUSE_SCROLL_SENSITIVITY);
484
485             // Camera distance clamp
486             if (CAMERA.targetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) CAMERA.targetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;
487
488             // TODO: It seems camera->position is not correctly updated or some rounding issue makes the camera move straight to camera->target...
489             camera->position.x = sinf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.x;
490
491             if (CAMERA.angle.y <= 0.0f) camera->position.y = sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y;
492             else camera->position.y = -sinf(CAMERA.angle.y)*CAMERA.targetDistance*sinf(CAMERA.angle.y) + camera->target.y;
493
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 keyPan) { CAMERA.panControl = keyPan; }
504
505 // Set camera alt key to combine with mouse movement (free camera)
506 void SetCameraAltControl(int keyAlt) { CAMERA.altControl = keyAlt; }
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 keyFront, int keyBack, int keyRight, int keyLeft, int keyUp, int keyDown)
513 {
514     CAMERA.moveControl[MOVE_FRONT] = keyFront;
515     CAMERA.moveControl[MOVE_BACK] = keyBack;
516     CAMERA.moveControl[MOVE_RIGHT] = keyRight;
517     CAMERA.moveControl[MOVE_LEFT] = keyLeft;
518     CAMERA.moveControl[MOVE_UP] = keyUp;
519     CAMERA.moveControl[MOVE_DOWN] = keyDown;
520 }
521
522 #endif // CAMERA_IMPLEMENTATION