You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.8 KiB

  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-2024 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 projection 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 }; // Ray collision hit info
  35. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. if (IsCursorHidden()) UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  43. // Toggle camera controls
  44. if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
  45. {
  46. if (IsCursorHidden()) EnableCursor();
  47. else DisableCursor();
  48. }
  49. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  50. {
  51. if (!collision.hit)
  52. {
  53. ray = GetScreenToWorldRay(GetMousePosition(), camera);
  54. // Check collision between ray and box
  55. collision = GetRayCollisionBox(ray,
  56. (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
  57. (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
  58. }
  59. else collision.hit = false;
  60. }
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. BeginMode3D(camera);
  67. if (collision.hit)
  68. {
  69. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED);
  70. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON);
  71. DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN);
  72. }
  73. else
  74. {
  75. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY);
  76. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY);
  77. }
  78. DrawRay(ray, MAROON);
  79. DrawGrid(10, 1.0f);
  80. EndMode3D();
  81. DrawText("Try clicking on the box with your mouse!", 240, 10, 20, DARKGRAY);
  82. if (collision.hit) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, (int)(screenHeight * 0.1f), 30, GREEN);
  83. DrawText("Right click mouse to toggle camera controls", 10, 430, 10, GRAY);
  84. DrawFPS(10, 10);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }