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.

105 lines
4.3 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()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. 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;
  21. camera.position = (Vector3){ 0.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. Vector3 cubePosition = { 0.0f, 1.0f, 0.0f };
  26. Vector3 cubeSize = { 2.0f, 2.0f, 2.0f };
  27. Ray ray; // Picking line ray
  28. bool collision = false;
  29. SetCameraMode(CAMERA_FREE); // Set a free camera mode
  30. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  31. SetCameraFovy(camera.fovy); // Set internal camera field-of-view Y
  32. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  33. //--------------------------------------------------------------------------------------
  34. // Main game loop
  35. while (!WindowShouldClose()) // Detect window close button or ESC key
  36. {
  37. // Update
  38. //----------------------------------------------------------------------------------
  39. UpdateCamera(&camera); // Update internal camera and our camera
  40. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  41. {
  42. // NOTE: This function is NOT WORKING properly!
  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. //----------------------------------------------------------------------------------
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(RAYWHITE);
  54. Begin3dMode(camera);
  55. if (collision)
  56. {
  57. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED);
  58. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON);
  59. DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN);
  60. }
  61. else
  62. {
  63. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY);
  64. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY);
  65. }
  66. DrawRay(ray, MAROON);
  67. DrawGrid(10, 1.0f);
  68. End3dMode();
  69. DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY);
  70. if(collision) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN);
  71. DrawFPS(10, 10);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. CloseWindow(); // Close window and OpenGL context
  78. //--------------------------------------------------------------------------------------
  79. return 0;
  80. }