FPS_Custom_Camera.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // If this will not conpile than look for the files raylib.h and raymath.h and copy these
  3. // into the folder where this file is. You can find these files in the raylib /src folder.
  4. //
  5. //
  6. #include "raylib.h"
  7. #include "raymath.h"
  8. #include <stdlib.h>
  9. #define PLAYER_MOVEMENT_SENSITIVITY 3
  10. #define CAMERA_MOUSE_MOVE_SENSITIVITY 0.01f
  11. static Vector2 lastMousePos;
  12. static Vector3 movement = {0, -0.2f, 0};
  13. static Vector3 rotation ;
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib example.");
  21. // We generate a checked image for texturing
  22. Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
  23. Texture2D texture = LoadTextureFromImage(checked);
  24. UnloadImage(checked);
  25. Model model = { 0 };
  26. model = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  27. // Set checked texture as default diffuse component for all models material
  28. model.materials[0].maps[MAP_DIFFUSE].texture = texture;
  29. // Define the camera to look into our 3d world
  30. Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  31. SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set camera mode
  32. //SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  33. camera.position = (Vector3){50.0f, 1.0f, 50.0f};
  34. camera.target = (Vector3){50.0f, 1.0f, 50.0f};
  35. camera.up = (Vector3){0.0f, 1.0f, 0.0f};
  36. camera.fovy = 60.0f;
  37. camera.type = CAMERA_PERSPECTIVE;
  38. rotation = (Vector3){0.0f, 0.0f, 0.0f};
  39. //world = world;
  40. lastMousePos = GetMousePosition();
  41. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  42. //--------------------------------------------------------------------------------------
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. //----------------------------------------------------------------------------------
  48. //UpdateCamera(&camera); // Update internal camera and our camera
  49. // -------------------- Rotation stuff --------------------
  50. Vector2 mouseMovement = Vector2Subtract(GetMousePosition(), lastMousePos);
  51. lastMousePos = GetMousePosition();
  52. rotation.x += (mouseMovement.x*-CAMERA_MOUSE_MOVE_SENSITIVITY);
  53. rotation.y += (mouseMovement.y*-CAMERA_MOUSE_MOVE_SENSITIVITY);
  54. // -------------------- Movement stuff --------------------
  55. float direction[] = { IsKeyDown(KEY_W),
  56. IsKeyDown(KEY_S),
  57. IsKeyDown(KEY_D),
  58. IsKeyDown(KEY_A)};
  59. //movement = (Vector3){0, movement.y + GetFrameTime() * -0.5, 0};
  60. movement = (Vector3){0, 0, 0};
  61. movement.x = (sinf(rotation.x)*direction[1] -
  62. sinf(rotation.x)*direction[0] -
  63. cosf(rotation.x)*direction[3] +
  64. cosf(rotation.x)*direction[2])/PLAYER_MOVEMENT_SENSITIVITY;
  65. movement.z = (cosf(rotation.x)*direction[1] -
  66. cosf(rotation.x)*direction[0] +
  67. sinf(rotation.x)*direction[3] -
  68. sinf(rotation.x)*direction[2])/PLAYER_MOVEMENT_SENSITIVITY;
  69. if (IsKeyDown(KEY_F))
  70. movement.y -= 0.12f;
  71. if (IsKeyDown(KEY_R))
  72. movement.y += 0.12f;
  73. camera.position.x += movement.x / PLAYER_MOVEMENT_SENSITIVITY;
  74. camera.position.y += movement.y;
  75. camera.position.z += movement.z / PLAYER_MOVEMENT_SENSITIVITY;
  76. // -------------------- Retarget stuff --------------------
  77. Matrix translation = MatrixTranslate(0, 0, (10));
  78. Matrix rotation2 = MatrixRotateXYZ((Vector3){ PI*2 - rotation.y, PI*2 - rotation.x, 0 });
  79. Matrix transform = MatrixMultiply(translation, rotation2);
  80. camera.target.x = camera.position.x - transform.m12;
  81. camera.target.y = camera.position.y - transform.m13;
  82. camera.target.z = camera.position.z - transform.m14;
  83. //----------------------------------------------------------------------------------
  84. // Draw
  85. //----------------------------------------------------------------------------------
  86. BeginDrawing();
  87. ClearBackground(RAYWHITE);
  88. BeginMode3D(camera);
  89. for(int x=0;x<50;x++){
  90. for(int z=0;z<50;z++){
  91. Vector3 position;
  92. position.x = x;
  93. position.y = 0;
  94. position.z = z;
  95. DrawModel(model, position, 1.0f, WHITE);
  96. }
  97. }
  98. EndMode3D();
  99. DrawText("Controls - Mouse w/a/s/d/ r/f.",400,0,20,DARKGRAY);
  100. DrawFPS(0,0);
  101. EndDrawing();
  102. //----------------------------------------------------------------------------------
  103. }
  104. // De-Initialization
  105. UnloadTexture(texture); // Unload texture
  106. // Unload models data (GPU VRAM)
  107. UnloadModel(model);
  108. //--------------------------------------------------------------------------------------
  109. CloseWindow(); // Close window and OpenGL context
  110. //--------------------------------------------------------------------------------------
  111. return 0;
  112. }