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.

103 lines
4.1 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){ 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. 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, CAMERA_FREE); // Set a free camera mode
  30. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  31. //--------------------------------------------------------------------------------------
  32. // Main game loop
  33. while (!WindowShouldClose()) // Detect window close button or ESC key
  34. {
  35. // Update
  36. //----------------------------------------------------------------------------------
  37. UpdateCamera(&camera); // Update camera
  38. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  39. {
  40. // NOTE: This function is NOT WORKING properly!
  41. ray = GetMouseRay(GetMousePosition(), camera);
  42. // Check collision between ray and box
  43. collision = CheckCollisionRayBox(ray,
  44. (BoundingBox){(Vector3){ cubePosition.x - cubeSize.x/2, cubePosition.y - cubeSize.y/2, cubePosition.z - cubeSize.z/2 },
  45. (Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
  46. }
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. Begin3dMode(camera);
  53. if (collision)
  54. {
  55. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, RED);
  56. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, MAROON);
  57. DrawCubeWires(cubePosition, cubeSize.x + 0.2f, cubeSize.y + 0.2f, cubeSize.z + 0.2f, GREEN);
  58. }
  59. else
  60. {
  61. DrawCube(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, GRAY);
  62. DrawCubeWires(cubePosition, cubeSize.x, cubeSize.y, cubeSize.z, DARKGRAY);
  63. }
  64. DrawRay(ray, MAROON);
  65. DrawGrid(10, 1.0f);
  66. End3dMode();
  67. DrawText("Try selecting the box with mouse!", 240, 10, 20, DARKGRAY);
  68. if(collision) DrawText("BOX SELECTED", (screenWidth - MeasureText("BOX SELECTED", 30)) / 2, screenHeight * 0.1f, 30, GREEN);
  69. DrawFPS(10, 10);
  70. EndDrawing();
  71. //----------------------------------------------------------------------------------
  72. }
  73. // De-Initialization
  74. //--------------------------------------------------------------------------------------
  75. CloseWindow(); // Close window and OpenGL context
  76. //--------------------------------------------------------------------------------------
  77. return 0;
  78. }