core_3d_picking.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Picking in 3d mode
  4. *
  5. * Example originally created with raylib 1.3, last time updated with raylib 4.0
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2015-2022 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [core] example - 3d picking");
  24. // Define the camera to look into our 3d world
  25. Camera camera = { 0 };
  26. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  27. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  28. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  29. camera.fovy = 45.0f; // Camera field-of-view Y
  30. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  31. Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
  32. Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
  33. Ray ray = { 0 }; // Picking line ray
  34. RayCollision collision = { 0 };
  35. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  36. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  37. //--------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. UpdateCamera(&camera);
  44. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  45. {
  46. if (!collision.hit)
  47. {
  48. ray = GetMouseRay(GetMousePosition(), camera);
  49. // Check collision between ray and box
  50. collision = GetRayCollisionBox(ray,
  51. (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
  52. (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
  53. }
  54. else collision.hit = false;
  55. }
  56. //----------------------------------------------------------------------------------
  57. // Draw
  58. //----------------------------------------------------------------------------------
  59. BeginDrawing();
  60. ClearBackground(RAYWHITE);
  61. BeginMode3D(camera);
  62. if (collision.hit)
  63. {
  64. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED);
  65. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON);
  66. DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN);
  67. }
  68. else
  69. {
  70. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY);
  71. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY);
  72. }
  73. DrawRay(ray, MAROON);
  74. DrawGrid(10, 1.0f);
  75. EndMode3D();
  76. DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY);
  77. if (collision.hit) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, (int)(screenHeight * 0.1f), 30, GREEN);
  78. DrawFPS(10, 10);
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. CloseWindow(); // Close window and OpenGL context
  85. //--------------------------------------------------------------------------------------
  86. return 0;
  87. }