Advertisement
Guest User

Raylib flying camera example

a guest
Jan 21st, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.32 KB | None | 0 0
  1. diff --git a/src/camera.h b/src/camera.h
  2. index 15958ddd..b1913823 100644
  3. --- a/src/camera.h
  4. +++ b/src/camera.h
  5. @@ -83,6 +83,7 @@
  6.          CAMERA_CUSTOM = 0,
  7.          CAMERA_FREE,
  8.          CAMERA_ORBITAL,
  9. +        CAMERA_FLYING,
  10.          CAMERA_FIRST_PERSON,
  11.          CAMERA_THIRD_PERSON
  12.      } CameraMode;
  13. @@ -164,6 +165,12 @@ void SetCameraMoveControls(int keyFront, int keyBack,
  14.  // ORBITAL_CAMERA
  15.  #define CAMERA_ORBITAL_SPEED                            0.01f       // Radians per frame
  16.  
  17. +// FLYING CAMERA
  18. +//#define CAMERA_FLYING_MOUSE_SENSITIVITY                 0.003f
  19. +#define CAMERA_FLYING_FOCUS_DISTANCE                    25.0f
  20. +#define CAMERA_FLYING_MIN_CLAMP                         89.0f
  21. +#define CAMERA_FLYING_MAX_CLAMP                        -89.0f
  22. +
  23.  // FIRST_PERSON
  24.  //#define CAMERA_FIRST_PERSON_MOUSE_SENSITIVITY           0.003f
  25.  #define CAMERA_FIRST_PERSON_FOCUS_DISTANCE              25.0f
  26. @@ -263,7 +270,11 @@ void SetCameraMode(Camera camera, int mode)
  27.      CAMERA.playerEyesPosition = camera.position.y;          // Init player eyes position to camera Y position
  28.  
  29.      // Lock cursor for first person and third person cameras
  30. -    if ((mode == CAMERA_FIRST_PERSON) || (mode == CAMERA_THIRD_PERSON)) DisableCursor();
  31. +    if (
  32. +      (mode == CAMERA_FLYING) ||
  33. +      (mode == CAMERA_FIRST_PERSON) ||
  34. +      (mode == CAMERA_THIRD_PERSON)
  35. +    ) DisableCursor();
  36.      else EnableCursor();
  37.  
  38.      CAMERA.mode = mode;
  39. @@ -411,6 +422,39 @@ void UpdateCamera(Camera *camera)
  40.              camera->position.z = cosf(CAMERA.angle.x)*CAMERA.targetDistance*cosf(CAMERA.angle.y) + camera->target.z;
  41.  
  42.          } break;
  43. +        case CAMERA_FLYING:   // Camera moves as in a first-person game but not locked to ground, controls are configurable
  44. +        {
  45. +            camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
  46. +                                   sinf(CAMERA.angle.x)*direction[MOVE_FRONT] -
  47. +                                   cosf(CAMERA.angle.x)*direction[MOVE_LEFT] +
  48. +                                   cosf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
  49. +
  50. +            camera->position.y += (sinf(CAMERA.angle.y)*direction[MOVE_FRONT] -
  51. +                                   sinf(CAMERA.angle.y)*direction[MOVE_BACK] +
  52. +                                   1.0f*direction[MOVE_UP] - 1.0f*direction[MOVE_DOWN])/PLAYER_MOVEMENT_SENSITIVITY;
  53. +
  54. +            camera->position.z += (cosf(CAMERA.angle.x)*direction[MOVE_BACK] -
  55. +                                   cosf(CAMERA.angle.x)*direction[MOVE_FRONT] +
  56. +                                   sinf(CAMERA.angle.x)*direction[MOVE_LEFT] -
  57. +                                   sinf(CAMERA.angle.x)*direction[MOVE_RIGHT])/PLAYER_MOVEMENT_SENSITIVITY;
  58. +
  59. +            // Camera orientation calculation
  60. +            CAMERA.angle.x += (mousePositionDelta.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
  61. +            CAMERA.angle.y += (mousePositionDelta.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
  62. +
  63. +            // Angle clamp
  64. +            if (CAMERA.angle.y > CAMERA_FLYING_MIN_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FLYING_MIN_CLAMP*DEG2RAD;
  65. +            else if (CAMERA.angle.y < CAMERA_FLYING_MAX_CLAMP*DEG2RAD) CAMERA.angle.y = CAMERA_FLYING_MAX_CLAMP*DEG2RAD;
  66. +
  67. +            // Recalculate camera target considering translation and rotation
  68. +            Matrix translation = MatrixTranslate(0, 0, (CAMERA.targetDistance/CAMERA_FREE_PANNING_DIVIDER));
  69. +            Matrix rotation = MatrixRotateXYZ((Vector3){ PI*2 - CAMERA.angle.y, PI*2 - CAMERA.angle.x, 0 });
  70. +            Matrix transform = MatrixMultiply(translation, rotation);
  71. +
  72. +            camera->target.x = camera->position.x - transform.m12;
  73. +            camera->target.y = camera->position.y - transform.m13;
  74. +            camera->target.z = camera->position.z - transform.m14;
  75. +        } break;
  76.          case CAMERA_FIRST_PERSON:   // Camera moves as in a first-person game, controls are configurable
  77.          {
  78.              camera->position.x += (sinf(CAMERA.angle.x)*direction[MOVE_BACK] -
  79. diff --git a/src/raylib.h b/src/raylib.h
  80. index 24992a4f..bf5baa4d 100644
  81. --- a/src/raylib.h
  82. +++ b/src/raylib.h
  83. @@ -855,6 +855,7 @@ typedef enum {
  84.      CAMERA_CUSTOM = 0,
  85.      CAMERA_FREE,
  86.      CAMERA_ORBITAL,
  87. +    CAMERA_FLYING,
  88.      CAMERA_FIRST_PERSON,
  89.      CAMERA_THIRD_PERSON
  90.  } CameraMode;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement