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.

107 lines
4.2 KiB

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