core_3d_picking.c 4.8 KB

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